<?php

declare(strict_types=1);
/**
 * 用户管理
 * @since   2018-02-06
 * @author  zhaoxiang <zhaoxiang051405@gmail.com>
 */

namespace app\controller\admin;

use app\model\AdminAuthGroupAccess;
use app\model\AdminBudget;
use app\model\AdminBudgetReport;
//use app\model\AdminUserData;
use app\model\AdminProject;
use app\util\ReturnCode;
use app\util\Tools;
use think\facade\Db;
use think\Response;
use PHPExcel_IOFactory;
use PHPExcel;
use PhpOffice\PhpWord\Shared\ZipArchive;

class Budget extends Base
{

    /**
     * 获取用户列表
     * @return Response
     * @throws \think\db\exception\DbException
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function index(): Response
    {
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $type = $this->request->get('type', '', 'intval');
        $keywords = $this->request->get('keywords', '');
        //        $status = $this->request->get('status', '');

        $obj = new AdminBudget();
        //        if (strlen($status)) {
        //            $obj = $obj->where('status', $status);
        //        }
        //        dump(!Tools::isAdministrator($this->userInfo['id']));
        if (!Tools::isAdministrator($this->userInfo['id'])) {
            // $obj = $obj->where('institutions_code', "{$keywords}");
            $dis_code = substr($this->userInfo['username'], 0, 6);
            $ins_code = substr($this->userInfo['username'], 6, 6);
            // $obj = $obj->where('institutions_code', "{$this->userInfo['username']}");
            $obj = $obj->where('institutions_code', $ins_code);
            $obj = $obj->where('district_code', $dis_code);
        }
        if ($keywords) {
            $obj = $obj->where('institutions_code', "{$keywords}");
        }
        $obj = $obj->where('type', "{$type}");

        $listObj = $obj->order('id', 'ASC')
            ->paginate(['page' => $start, 'list_rows' => $limit])->each(function ($item, $key) {
                $item->userData;
            })->toArray();

        $listInfo = $listObj['data'];

        return $this->buildSuccess([
            'list'  => $listInfo,
            'count' => $listObj['total']
        ]);
    }

    /**
     * 获取用户列表
     * @return Response
     * @throws \think\db\exception\DbException
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function edit(): Response
    {
        $postData = $this->request->post();

        $res = AdminBudget::update($postData);

        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }

        return $this->buildSuccess();
    }
    /**
     * 获取用户列表
     * @return Response
     * @throws \think\db\exception\DbException
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function report(): Response
    {
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $status = $this->request->get('status', '');
        $depart = $this->request->get('depart', '');
        $keywords = $this->request->get('keywords', '');
        //        $status = $this->request->get('status', '');

        $obj = new AdminBudgetReport();
        //        if (strlen($status)) {
        //            $obj = $obj->where('status', $status);
        //        }
        //        dump(!Tools::isAdministrator($this->userInfo['id']));
        if (!Tools::isAdministrator($this->userInfo['id'])) {
            // $obj = $obj->where('institutions_code', "{$this->userInfo['username']}");
            $dis_code = substr($this->userInfo['username'], 0, 6);
            $ins_code = substr($this->userInfo['username'], 6, 6);
            // $obj = $obj->where('institutions_code', "{$this->userInfo['username']}");
            $obj = $obj->where('institutions_code', $ins_code);
            $obj = $obj->where('district_code', $dis_code);
        }
        if ($keywords) {
            // $obj = $obj->where('institutions_code', "{$keywords}");
            $obj = $obj->where([
                ['institutions_name', 'like', "%{$keywords}%"]
            ]);
        }
        if ($status != '') {
            $obj = $obj->where("status", $status);
        }

        if ($depart != '') {
            $obj = $obj->where("departments_code", $depart);
        }

        $listObj = $obj->order('id', 'ASC')
            ->paginate(['page' => $start, 'list_rows' => $limit])->each(function ($item, $key) {
                $item->userData;
            })->toArray();

        $listInfo = $listObj['data'];

        return $this->buildSuccess([
            'list'  => $listInfo,
            'count' => $listObj['total']
        ]);
    }

    /**
     * 获取用户列表
     * @return Response
     * @throws \think\db\exception\DbException
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function getReportData(): Response
    {
        $code = $this->request->get('code', '');
        $dis_code = substr($code, 0, 6);
        $ins_code = substr($code, 6, 6);

        $obj = new AdminBudgetReport();
        $listObj = $obj->where('uniq_code', "{$code}")->find()->toArray();

        //table1单位收支总体情况
        //收入
        $countObj = new AdminBudget();
        $countObj = $countObj->where('type', "1");
        $countData = $countObj
            ->field('sum(fiscal_appropriation) fiscal_appropriation_count,sum(general_public_budget) general_public_budget_count,sum(government_budgets) government_budgets_count,sum(government_capital_management_budget) government_capital_management_budget_count,sum(special_financial_account) special_financial_account_count,sum(utility_income) utility_income_count,sum(institution_management_income) institution_management_income_count,sum(institution_other_income) institution_other_income_count')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])->find()->toArray();
        $countData['income_total'] = $countData['fiscal_appropriation_count'] + $countData['special_financial_account_count'] + $countData['utility_income_count'] + $countData['institution_management_income_count'] + $countData['institution_other_income_count']; //收入合计
        //支出
        $budgetObj = new AdminBudget();
        $budgetObj = $budgetObj->where('type', "1");
        $budgetData = $budgetObj
            ->field('functional_category_code_class,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class')
            ->select()->toArray();
        $budgetData = array_combine(array_column($budgetData, 'functional_category_code_class'), array_column($budgetData, 'total'));
        $budgetData['budget_total'] = array_sum($budgetData); //支出合计

        //table2单位收入总体情况表
        $countClassObj = new AdminBudget();
        $countClassObj = $countClassObj->where('type', "1");
        $countClassData = $countClassObj
            ->field('functional_category_code_class,functional_category_name_class name,sum(general_public_budget) general_public_budget_count,sum(government_budgets) government_budgets_count,sum(government_capital_management_budget) government_capital_management_budget_count,sum(special_financial_account) special_financial_account_count,sum(utility_income) utility_income_count,sum(institution_management_income) institution_management_income_count,sum(institution_other_income) institution_other_income_count,economics_category_name_class,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class')
            ->orderRaw('functional_category_code_class*1 ASC')->select()->toArray();
        $countFundObj = new AdminBudget();
        $countFundObj = $countFundObj->where('type', "1");
        $countFundData = $countFundObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_name_fund name,sum(general_public_budget) general_public_budget_count,sum(government_budgets) government_budgets_count,sum(government_capital_management_budget) government_capital_management_budget_count,sum(special_financial_account) special_financial_account_count,sum(utility_income) utility_income_count,sum(institution_management_income) institution_management_income_count,sum(institution_other_income) institution_other_income_count,economics_category_name_class,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class,functional_category_code_fund')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC')->select()->toArray();

        $countItemObj = new AdminBudget();
        $countItemObj = $countItemObj->where('type', "1");
        $countItemData = $countItemObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_code_item,functional_category_name_item name,sum(general_public_budget) general_public_budget_count,sum(government_budgets) government_budgets_count,sum(government_capital_management_budget) government_capital_management_budget_count,sum(special_financial_account) special_financial_account_count,sum(utility_income) utility_income_count,sum(institution_management_income) institution_management_income_count,sum(institution_other_income) institution_other_income_count,economics_category_name_class,project_name,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class,functional_category_code_fund,functional_category_code_item')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC,functional_category_code_item*1 ASC')->select()->toArray();

        $countDetailData = [];
        $general_public_budget_count_total = 0;
        $government_budgets_count_total = 0;
        $government_capital_management_budget_count_total = 0;
        $special_financial_account_count_total = 0;
        $utility_income_count_total = 0;
        $institution_management_income_count_total = 0;
        $institution_other_income_count_total = 0;
        $base_budget_total = 0;
        $project_budget_total = 0;
        $income_total = 0;
        $budget_total = 0;
        $general_public_base_budget_total = 0;
        $general_public_project_budget_total = 0;
        foreach ($countClassData as $v) {
            $v['income_total'] = $v['general_public_budget_count'] + $v['government_budgets_count'] + $v['government_capital_management_budget_count'] + $v['special_financial_account_count'] + $v['utility_income_count'] + $v['institution_management_income_count'] + $v['institution_other_income_count'];
            $countDetailData[] = $v;
            $income_total += $v['income_total'];
            $budget_total += $v['total'];
            $general_public_budget_count_total += $v['general_public_budget_count'];
            $government_budgets_count_total += $v['government_budgets_count'];
            $government_capital_management_budget_count_total += $v['government_capital_management_budget_count'];
            $special_financial_account_count_total += $v['special_financial_account_count'];
            $utility_income_count_total += $v['utility_income_count'];
            $institution_management_income_count_total += $v['institution_management_income_count'];
            $institution_other_income_count_total += $v['institution_other_income_count'];
            if ($v['project_category_code'] == 1) {
                $base_budget_total += $v['total'];
                if ($v['general_public_budget_count'] > 0) {
                    $general_public_base_budget_total += $v['total'];
                }
            }
            if ($v['project_category_code'] == 2) {
                $project_budget_total += $v['total'];
                if ($v['general_public_budget_count'] > 0) {
                    $general_public_project_budget_total += $v['total'];
                }
            }
            foreach ($countFundData as $j) {
                if ($j['functional_category_code_class'] == $v['functional_category_code_class']) {
                    $j['income_total'] = $j['general_public_budget_count'] + $j['government_budgets_count'] + $j['government_capital_management_budget_count'] + $j['special_financial_account_count'] + $j['utility_income_count'] + $j['institution_management_income_count'] + $j['institution_other_income_count'];
                    $countDetailData[] = $j;
                    foreach ($countItemData as $l) {
                        if ($l['functional_category_code_class'] == $v['functional_category_code_class'] && $l['functional_category_code_fund'] == $j['functional_category_code_fund']) {
                            $l['income_total'] = $l['general_public_budget_count'] + $l['government_budgets_count'] + $l['government_capital_management_budget_count'] + $l['special_financial_account_count'] + $l['utility_income_count'] + $l['institution_management_income_count'] + $l['institution_other_income_count'];
                            $countDetailData[] = $l;
                        }
                    }
                }
            }
        }

        //table5
        $general_public_budget_data = [];
        foreach ($countDetailData as $v) {
            if ($v['general_public_budget_count'] > 0) {
                $v['total'] = $v['total'] / 10000;
                $general_public_budget_data[] = $v;
            }
        }

        //经济分类数据table6
        $economicsClassObj = new AdminBudget();
        $economicsClassObj = $economicsClassObj->where('type', "1");
        $economicsClassData = $economicsClassObj
            ->field('total,economics_category_code_class,economics_category_name_class,economics_category_name_fund name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('economics_category_code_class')->orderRaw('economics_category_code_class*1 ASC')->select()->toArray();
        $economicsFundObj = new AdminBudget();
        $economicsFundObj = $economicsFundObj->where('type', "1");
        $economicsFundData = $economicsFundObj
            ->field('total,economics_category_code_class,economics_category_name_class,economics_category_code_fund,economics_category_name_fund name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('economics_category_code_class,economics_category_code_fund')->orderRaw('economics_category_code_class*1 ASC,economics_category_code_fund*1 ASC')->select()->toArray();

        $economicsDetailData = [];
        $economicsDetailTotal = 0;
        $economicsPeopleTotal = 0;
        $economicsPublicTotal = 0;
        foreach ($economicsClassData as $v) {
            $economicsDetailData[] = $v;
            $economicsDetailTotal += $v['total'];
            if ($v['economics_category_code_class'] == '301' || $v['economics_category_code_class'] == '303') {
                $economicsPeopleTotal += $v['total'];
            }
            if ($v['economics_category_code_class'] == '302') {
                $economicsPublicTotal += $v['total'];
            }
            foreach ($economicsFundData as $j) {
                if ($j['economics_category_code_class'] == $v['economics_category_code_class']) {
                    $economicsDetailData[] = $j;
                }
            }
        }

        //table7一般公共预算项目支出情况表
        $countProjectNameObj = new AdminBudget();
        $countProjectNameObj = $countProjectNameObj->where('type', "1");
        $countProjectNameObj = $countProjectNameObj->where('project_category_code', "2");
        $countProjectNameData = $countProjectNameObj
            ->field('functional_category_code_class,functional_category_name_class name,economics_category_name_class,project_category_code,project_category_name,economics_category_code,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class,economics_category_name_class')
            ->orderRaw('functional_category_code_class*1 ASC')->select()->toArray();
        $countProjectNameDetailData = [];
        foreach ($countProjectNameData as $v) {
            $countProjectNameDetailData[$v['functional_category_code_class']][$v['economics_category_code']] = $v;
        }

        $countProjectClassObj = new AdminBudget();
        $countProjectClassObj = $countProjectClassObj->where('type', "1");
        $countProjectClassObj = $countProjectClassObj->where('project_category_code', "2");
        $countProjectClassData = $countProjectClassObj
            ->field('functional_category_code_class,functional_category_name_class name,economics_category_name_class,project_category_code,project_category_name,economics_category_code,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class')
            ->orderRaw('functional_category_code_class*1 ASC')->select()->toArray();

        $countProjectFundObj = new AdminBudget();
        $countProjectFundObj = $countProjectFundObj->where('type', "1");
        $countProjectFundObj = $countProjectFundObj->where('project_category_code', "2");
        $countProjectFundData = $countProjectFundObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_name_fund name,economics_category_name_class,project_category_code,project_category_name,economics_category_code,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class,functional_category_code_fund')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC')->select()->toArray();

        $countProjectItemObj = new AdminBudget();
        $countProjectItemObj = $countProjectItemObj->where('type', "1");
        $countProjectItemObj = $countProjectItemObj->where('project_category_code', "2");
        $countProjectItemData = $countProjectItemObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_code_item,functional_category_name_item name,economics_category_name_class,project_name,project_category_code,project_category_name,economics_category_code,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class,functional_category_code_fund,functional_category_code_item')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC,functional_category_code_item*1 ASC')->select()->toArray();

        $countProjectData = [];
        foreach ($countProjectClassData as $v) {
            $countProjectData[] = $v;
            foreach ($countProjectFundData as $j) {
                if ($j['functional_category_code_class'] == $v['functional_category_code_class']) {
                    $countProjectData[] = $j;
                    foreach ($countProjectItemData as $l) {
                        if ($l['functional_category_code_class'] == $v['functional_category_code_class'] && $l['functional_category_code_fund'] == $j['functional_category_code_fund']) {
                            $countProjectData[] = $l;
                        }
                    }
                }
            }
        }

        //table8三公支出费用
        $threePublicObj = new AdminBudget();
        $threePublicObj = $threePublicObj->where('type', "1");
        $threePublicObj = $threePublicObj->where('economics_category_code', 'in', "30212,30913,31013,30231,30217");
        $threePublic = $threePublicObj
            ->field('economics_category_code,functional_category_code_class,functional_category_name_class name,economics_category_name_class,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('economics_category_code_fund')
            ->orderRaw('economics_category_code_fund*1 ASC')->select()->toArray();
        $threePublic = array_combine(array_column($threePublic, 'economics_category_code'), array_column($threePublic, 'total'));
        $threePublic['total'] = array_sum($threePublic);
        //        var_dump($threePublicObj->getLastSql());

        //table9政府性基金预算支出情况表
        $countGovernmentClassObj = new AdminBudget();
        $countGovernmentClassObj = $countGovernmentClassObj->where('type', "1");
        $countGovernmentClassData = $countGovernmentClassObj
            ->field('functional_category_code_class,functional_category_name_class,economics_category_name_class,project_category_code,project_category_name,sum(total) total,sum(government_budgets) government_budgets_total,government_budgets,functional_category_name name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('government_budgets', '>', 0)
            ->group('functional_category_code_class')
            ->orderRaw('functional_category_code_class*1 ASC')->select()->toArray();
        $countGovernmentFundObj = new AdminBudget();
        $countGovernmentFundObj = $countGovernmentFundObj->where('type', "1");
        $countGovernmentFundData = $countGovernmentFundObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_name_fund,economics_category_name_class,project_category_code,project_category_name,sum(total) total,sum(government_budgets) government_budgets_total,government_budgets,functional_category_name name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('government_budgets', '>', 0)
            ->group('functional_category_code_class,functional_category_code_fund')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC')->select()->toArray();

        $countGovernmentItemObj = new AdminBudget();
        $countGovernmentItemObj = $countGovernmentItemObj->where('type', "1");
        $countGovernmentItemData = $countGovernmentItemObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_code_item,functional_category_name_item,economics_category_name_class,project_name,project_category_code,project_category_name,sum(total) total,sum(government_budgets) government_budgets_total,government_budgets,functional_category_name name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('government_budgets', '>', 0)
            ->group('functional_category_code_class,functional_category_code_fund,functional_category_code_item')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC,functional_category_code_item*1 ASC')->select()->toArray();

        $countGovernmentData = [];
        $countGovernmentTotal = 0;
        $countGovernmentProjectTotal = 0;
        $countGovernmentBaseTotal = 0;
        foreach ($countGovernmentClassData as $v) {
            $countGovernmentData[] = $v;
            $countGovernmentTotal += $v['government_budgets_total'];
            if ($v['project_category_code'] == 1) {
                $countGovernmentBaseTotal += $v['government_budgets_total'];
            }
            if ($v['project_category_code'] == 2) {
                $countGovernmentProjectTotal += $v['government_budgets_total'];
            }
            foreach ($countGovernmentFundData as $j) {
                if ($j['functional_category_code_class'] == $v['functional_category_code_class']) {
                    $countGovernmentData[] = $j;
                    foreach ($countGovernmentItemData as $l) {
                        if ($l['functional_category_code_class'] == $v['functional_category_code_class'] && $l['functional_category_code_fund'] == $j['functional_category_code_fund']) {
                            $countGovernmentData[] = $l;
                        }
                    }
                }
            }
        }

        //机关运行经费
        $officeRunFundingObj = new AdminBudget();
        $officeRunFundingObj = $officeRunFundingObj->where('type', "1");
        $officeRunFunding = $officeRunFundingObj->field('economics_category_name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('economics_category_code_class', '302')
            ->where('fiscal_appropriation', '>', 0)
            ->group('economics_category_code,economics_category_name')->column('economics_category_name');
        array_unique($officeRunFunding);


        $projectObj = new AdminProject();
        // $projectList = $projectObj->where(['institutions_code'=>$ins_code,'district_code'=>$dis_code])->select()->toArray();
        $projectList = $projectObj->where(['uniq_code' => $code])->select()->toArray();

        //处理数据
        foreach ($countData as $k => $v) {
            $countData[$k] = $v / 10000;
        }
        foreach ($budgetData as $k => $v) {
            $budgetData[$k] = $v / 10000;
        }
        foreach ($countDetailData as $k => $v) {
            $v['total'] = $v['total'] / 10000;
            $v['income_total'] = $v['income_total'] / 10000;
            $v['general_public_budget_count'] = $v['general_public_budget_count'] / 10000;
            $v['government_budgets_count'] = $v['government_budgets_count'] / 10000;
            $v['government_capital_management_budget_count'] = $v['government_capital_management_budget_count'] / 10000;
            $v['special_financial_account_count'] = $v['special_financial_account_count'] / 10000;
            $v['utility_income_count'] = $v['utility_income_count'] / 10000;
            $v['institution_management_income_count'] = $v['institution_management_income_count'] / 10000;
            $v['institution_other_income_count'] = $v['institution_other_income_count'] / 10000;
            $countDetailData[$k] = $v;
        }
        foreach ($countProjectData as $k => $v) {
            $v['total'] = $v['total'] / 10000;
            $countProjectData[$k] = $v;
        }
        foreach ($economicsDetailData as $k => $v) {
            $v['total'] = $v['total'] / 10000;
            $economicsDetailData[$k] = $v;
        }

        foreach ($threePublic as $k => $v) {
            $threePublic[$k] = $v / 10000;
        }

        $listInfo['obj'] = $listObj;
        $listInfo['incomeCount'] = $countData;
        $listInfo['budgetCount'] = $budgetData;
        $listInfo['countDetailData'] = [
            'list' => $countDetailData,
            'general_public_budget_count_total' => $general_public_budget_count_total / 10000,
            'government_budgets_count_total' => $government_budgets_count_total / 10000,
            'government_capital_management_budget_count_total' => $government_capital_management_budget_count_total / 10000,
            'special_financial_account_count_total' => $special_financial_account_count_total / 10000,
            'utility_income_count_total' => $utility_income_count_total / 10000,
            'institution_management_income_count_total' => $institution_management_income_count_total / 10000,
            'institution_other_income_count_total' => $institution_other_income_count_total / 10000,
            'base_budget_total' => $base_budget_total / 10000,
            'project_budget_total' => $project_budget_total / 10000,
            'income_total' => $income_total / 10000,
            'budget_total' => $budget_total / 10000,
            'general_public_base_budget_total' => $general_public_base_budget_total / 10000,
            'general_public_project_budget_total' => $general_public_project_budget_total / 10000,
        ];
        $listInfo['countClassData'] = $countClassData;
        $listInfo['countFundData'] = $countFundData;
        $listInfo['countItemData'] = $countItemData;
        $listInfo['general_public_budget_data'] = $general_public_budget_data;
        $listInfo['economicsDetailData'] = [
            'list' => $economicsDetailData,
            'economicsDetailTotal' => $economicsDetailTotal / 10000,
            'economicsPeopleTotal' => $economicsPeopleTotal / 10000,
            'economicsPublicTotal' => $economicsPublicTotal / 10000
        ];
        $listInfo['countProjectData'] = [
            'list' => $countProjectData,
            'countProjectNameDetailData' => $countProjectNameDetailData
        ];
        $listInfo['threePublic'] = $threePublic;
        $listInfo['countGovernmentData'] = [
            'list' => $countGovernmentData,
            'countGovernmentTotal' => $countGovernmentTotal,
            'countGovernmentProjectTotal' => $countGovernmentProjectTotal,
            'countGovernmentBaseTotal' => $countGovernmentBaseTotal,
        ];
        $listInfo['officeRunFunding'] = $officeRunFunding;
        $listInfo['projectList'] = $projectList;

        return $this->buildSuccess($listInfo);
    }

    /**
     * 获取用户列表
     * @return Response
     * @throws \think\db\exception\DbException
     * @author zhaoxiang <zhaoxiang051405@gmail.com>
     */
    public function saveReport(): Response
    {
        $postData = $this->request->post();
        //        $report = $postData['report'];
        //        if($postData['is_submit']==1){
        //            $report['status'] = 2;
        //        }
        //        $report['office_data'] = json_encode($postData['report']['office_data']);
        //        $report['staffing_data'] = json_encode($postData['report']['staffing_data']);
        //        $report['subordinate_unit_data'] = json_encode($postData['report']['subordinate_unit_data']);
        //        $res = AdminBudgetReport::update($report);
        //        var_dump($postData);
        Db::startTrans();
        try {
            $obj = new AdminBudgetReport();
            if (isset($postData['is_cancel']) && $postData['is_cancel'] == 1) {
                $report['id'] = $postData['id'];
                $report['status'] = 1;
                $obj->update($report);
            } else {
                $report = $postData['report'];
                if (isset($postData['is_submit']) && $postData['is_submit'] == 1) {
                    $report['status'] = 2;
                }
                $report['office_data'] = json_encode($postData['report']['office_data']);
                $report['staffing_data'] = json_encode($postData['report']['staffing_data']);
                $report['subordinate_unit_data'] = json_encode($postData['report']['subordinate_unit_data']);
                $obj->update($report);
            }
            if (isset($postData['project'])) {
                $projectObj = new AdminProject();
                $projectObj->saveAll($postData['project']);
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }
        //        if ($res === false) {
        //            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        //        }
        return $this->buildSuccess();
    }
    public function import(): Response
    {
        $file_name = root_path() . 'public/uploads/1111.xlsx';   //上传文件的地址
        $name = strrchr('1111.xlsx', '.');
        if ($name == '.xlsx') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel2007');
        } elseif ($name == '.xls') {
            $objReader = \PHPExcel_IOFactory::createReader('Excel5');
        } elseif ($name == '.csv') {
            $objReader = \PHPExcel_IOFactory::createReader('csv');
        }
        $obj_PHPExcel = $objReader->load($file_name, $encode = 'utf-8');  //加载文件内容,编码utf-8

        $excel_array = $obj_PHPExcel->getsheet(0)->toArray();   //转换为数组格式
        // array_shift($excel_array);  //删除第一个数组(标题);
        foreach ($excel_array as $k => $v) {
            $insert = [
                'district_code' => $v[1],
                'district_name' => $v[2],
                'institutions_code' => $v[3],
                'institutions_name' => $v[4],
                'departments_code' => $v[5],
                'departments_name' => $v[6],
                'project_category_code' => $v[7],
                'project_category_name' => $v[8],
                'project_code' => $v[9],
                'project_name' => $v[10],
                'functional_category_code' => $v[11],
                'functional_category_name' => $v[12],
                'functional_category_code_class' => $v[13],
                'functional_category_name_class' => $v[14],
                'functional_category_code_fund' => $v[15],
                'functional_category_name_fund' => $v[16],
                'functional_category_code_item' => $v[17],
                'functional_category_name_item' => $v[18],
                'economics_category_code' => $v[19],
                'economics_category_name' => $v[20],
                'economics_category_code_class' => $v[21],
                'economics_category_name_class' => $v[22],
                'economics_category_code_fund' => $v[23],
                'economics_category_name_fund' => $v[24],
                'total' => $v[25],
                'fiscal_appropriation' => $v[26],
                'general_public_budget' => $v[27],
                'government_budgets' => $v[28],
                'government_capital_management_budget' => $v[29],
                'special_financial_account' => $v[30],
                'utility_income' => $v[31],
                'institution_management_income' => $v[32],
                'institution_other_income' => $v[33],
                'type' => $v[34],
            ];
            $insertData[] = $insert;
        }
        (new AdminBudget())->insertAll($insertData);

        return $this->buildSuccess();
    }
    public function getReportDetailData($code)
    {
        $dis_code = substr($code, 0, 6);
        $ins_code = substr($code, 6, 6);
        $obj = new AdminBudgetReport();
        // $listObj = $obj->where(['institutions_code'=>$ins_code,'district_code'=>$dis_code])->find()->toArray();
        $listObj = $obj->where(['uniq_code' => $code])->find()->toArray();

        //table1单位收支总体情况
        //收入
        $countObj = new AdminBudget();
        $countObj = $countObj->where('type', "1");
        $countData = $countObj
            ->field('sum(fiscal_appropriation) fiscal_appropriation_count,sum(general_public_budget) general_public_budget_count,sum(government_budgets) government_budgets_count,sum(government_capital_management_budget) government_capital_management_budget_count,sum(special_financial_account) special_financial_account_count,sum(utility_income) utility_income_count,sum(institution_management_income) institution_management_income_count,sum(institution_other_income) institution_other_income_count')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])->find()->toArray();
        $countData['income_total'] = $countData['fiscal_appropriation_count'] + $countData['special_financial_account_count'] + $countData['utility_income_count'] + $countData['institution_management_income_count'] + $countData['institution_other_income_count']; //收入合计
        //支出
        $budgetObj = new AdminBudget();
        $budgetObj = $budgetObj->where('type', "1");
        $budgetData = $budgetObj
            ->field('functional_category_code_class,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class')
            ->select()->toArray();
        $budgetData = array_combine(array_column($budgetData, 'functional_category_code_class'), array_column($budgetData, 'total'));
        $budgetData['budget_total'] = array_sum($budgetData); //支出合计

        //table2单位收入总体情况表
        $countClassObj = new AdminBudget();
        $countClassObj = $countClassObj->where('type', "1");
        $countClassData = $countClassObj
            ->field('functional_category_code_class,functional_category_name_class name,sum(general_public_budget) general_public_budget_count,sum(government_budgets) government_budgets_count,sum(government_capital_management_budget) government_capital_management_budget_count,sum(special_financial_account) special_financial_account_count,sum(utility_income) utility_income_count,sum(institution_management_income) institution_management_income_count,sum(institution_other_income) institution_other_income_count,economics_category_name_class,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class')
            ->orderRaw('functional_category_code_class*1 ASC')->select()->toArray();
        $countFundObj = new AdminBudget();
        $countFundObj = $countFundObj->where('type', "1");
        $countFundData = $countFundObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_name_fund name,sum(general_public_budget) general_public_budget_count,sum(government_budgets) government_budgets_count,sum(government_capital_management_budget) government_capital_management_budget_count,sum(special_financial_account) special_financial_account_count,sum(utility_income) utility_income_count,sum(institution_management_income) institution_management_income_count,sum(institution_other_income) institution_other_income_count,economics_category_name_class,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class,functional_category_code_fund')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC')->select()->toArray();

        $countItemObj = new AdminBudget();
        $countItemObj = $countItemObj->where('type', "1");
        $countItemData = $countItemObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_code_item,functional_category_name_item name,sum(general_public_budget) general_public_budget_count,sum(government_budgets) government_budgets_count,sum(government_capital_management_budget) government_capital_management_budget_count,sum(special_financial_account) special_financial_account_count,sum(utility_income) utility_income_count,sum(institution_management_income) institution_management_income_count,sum(institution_other_income) institution_other_income_count,economics_category_name_class,project_name,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class,functional_category_code_fund,functional_category_code_item')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC,functional_category_code_item*1 ASC')->select()->toArray();

        $countDetailData = [];
        $general_public_budget_count_total = 0;
        $government_budgets_count_total = 0;
        $government_capital_management_budget_count_total = 0;
        $special_financial_account_count_total = 0;
        $utility_income_count_total = 0;
        $institution_management_income_count_total = 0;
        $institution_other_income_count_total = 0;
        $base_budget_total = 0;
        $project_budget_total = 0;
        $income_total = 0;
        $budget_total = 0;
        $general_public_base_budget_total = 0;
        $general_public_project_budget_total = 0;
        foreach ($countClassData as $v) {
            $v['income_total'] = $v['general_public_budget_count'] + $v['government_budgets_count'] + $v['government_capital_management_budget_count'] + $v['special_financial_account_count'] + $v['utility_income_count'] + $v['institution_management_income_count'] + $v['institution_other_income_count'];
            $countDetailData[] = $v;
            $income_total += $v['income_total'];
            $budget_total += $v['total'];
            $general_public_budget_count_total += $v['general_public_budget_count'];
            $government_budgets_count_total += $v['government_budgets_count'];
            $government_capital_management_budget_count_total += $v['government_capital_management_budget_count'];
            $special_financial_account_count_total += $v['special_financial_account_count'];
            $utility_income_count_total += $v['utility_income_count'];
            $institution_management_income_count_total += $v['institution_management_income_count'];
            $institution_other_income_count_total += $v['institution_other_income_count'];
            if ($v['project_category_code'] == 1) {
                $base_budget_total += $v['total'];
                if ($v['general_public_budget_count'] > 0) {
                    $general_public_base_budget_total += $v['total'];
                }
            }
            if ($v['project_category_code'] == 2) {
                $project_budget_total += $v['total'];
                if ($v['general_public_budget_count'] > 0) {
                    $general_public_project_budget_total += $v['total'];
                }
            }
            foreach ($countFundData as $j) {
                if ($j['functional_category_code_class'] == $v['functional_category_code_class']) {
                    $j['income_total'] = $j['general_public_budget_count'] + $j['government_budgets_count'] + $j['government_capital_management_budget_count'] + $j['special_financial_account_count'] + $j['utility_income_count'] + $j['institution_management_income_count'] + $j['institution_other_income_count'];
                    $countDetailData[] = $j;
                    foreach ($countItemData as $l) {
                        if ($l['functional_category_code_class'] == $v['functional_category_code_class'] && $l['functional_category_code_fund'] == $j['functional_category_code_fund']) {
                            $l['income_total'] = $l['general_public_budget_count'] + $l['government_budgets_count'] + $l['government_capital_management_budget_count'] + $l['special_financial_account_count'] + $l['utility_income_count'] + $l['institution_management_income_count'] + $l['institution_other_income_count'];
                            $countDetailData[] = $l;
                        }
                    }
                }
            }
        }

        //table5
        $general_public_budget_data = [];
        foreach ($countDetailData as $v) {
            if ($v['general_public_budget_count'] > 0) {
                $v['total'] = $v['total'] / 10000;
                $general_public_budget_data[] = $v;
            }
        }

        //经济分类数据table6
        $economicsClassObj = new AdminBudget();
        $economicsClassObj = $economicsClassObj->where('type', "1");
        $economicsClassData = $economicsClassObj
            ->field('total,economics_category_code_class,economics_category_name_class,economics_category_name_fund name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('economics_category_code_class')->orderRaw('economics_category_code_class*1 ASC')->select()->toArray();
        $economicsFundObj = new AdminBudget();
        $economicsFundObj = $economicsFundObj->where('type', "1");
        $economicsFundData = $economicsFundObj
            ->field('total,economics_category_code_class,economics_category_name_class,economics_category_code_fund,economics_category_name_fund name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('economics_category_code_class,economics_category_code_fund')->orderRaw('economics_category_code_class*1 ASC,economics_category_code_fund*1 ASC')->select()->toArray();

        $economicsDetailData = [];
        $economicsDetailTotal = 0;
        $economicsPeopleTotal = 0;
        $economicsPublicTotal = 0;
        foreach ($economicsClassData as $v) {
            $economicsDetailData[] = $v;
            $economicsDetailTotal += $v['total'];
            if ($v['economics_category_code_class'] == '301' || $v['economics_category_code_class'] == '303') {
                $economicsPeopleTotal += $v['total'];
            }
            if ($v['economics_category_code_class'] == '302') {
                $economicsPublicTotal += $v['total'];
            }
            foreach ($economicsFundData as $j) {
                if ($j['economics_category_code_class'] == $v['economics_category_code_class']) {
                    $economicsDetailData[] = $j;
                }
            }
        }

        //table7一般公共预算项目支出情况表
        $countProjectNameObj = new AdminBudget();
        $countProjectNameObj = $countProjectNameObj->where('type', "1");
        $countProjectNameObj = $countProjectNameObj->where('project_category_code', "2");
        $countProjectNameData = $countProjectNameObj
            ->field('functional_category_code_class,functional_category_name_class name,economics_category_name_class,project_category_code,project_category_name,economics_category_code,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class,economics_category_name_class')
            ->orderRaw('functional_category_code_class*1 ASC')->select()->toArray();
        $countProjectNameDetailData = [];
        foreach ($countProjectNameData as $v) {
            $countProjectNameDetailData[$v['functional_category_code_class']][$v['economics_category_code']] = $v;
        }

        $countProjectClassObj = new AdminBudget();
        $countProjectClassObj = $countProjectClassObj->where('type', "1");
        $countProjectClassObj = $countProjectClassObj->where('project_category_code', "2");
        $countProjectClassData = $countProjectClassObj
            ->field('functional_category_code_class,functional_category_name_class name,economics_category_name_class,project_category_code,project_category_name,economics_category_code,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class')
            ->orderRaw('functional_category_code_class*1 ASC')->select()->toArray();

        $countProjectFundObj = new AdminBudget();
        $countProjectFundObj = $countProjectFundObj->where('type', "1");
        $countProjectFundObj = $countProjectFundObj->where('project_category_code', "2");
        $countProjectFundData = $countProjectFundObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_name_fund name,economics_category_name_class,project_category_code,project_category_name,economics_category_code,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class,functional_category_code_fund')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC')->select()->toArray();

        $countProjectItemObj = new AdminBudget();
        $countProjectItemObj = $countProjectItemObj->where('type', "1");
        $countProjectItemObj = $countProjectItemObj->where('project_category_code', "2");
        $countProjectItemData = $countProjectItemObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_code_item,functional_category_name_item name,economics_category_name_class,project_name,project_category_code,project_category_name,economics_category_code,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('functional_category_code_class,functional_category_code_fund,functional_category_code_item')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC,functional_category_code_item*1 ASC')->select()->toArray();

        $countProjectData = [];
        foreach ($countProjectClassData as $v) {
            $countProjectData[] = $v;
            foreach ($countProjectFundData as $j) {
                if ($j['functional_category_code_class'] == $v['functional_category_code_class']) {
                    $countProjectData[] = $j;
                    foreach ($countProjectItemData as $l) {
                        if ($l['functional_category_code_class'] == $v['functional_category_code_class'] && $l['functional_category_code_fund'] == $j['functional_category_code_fund']) {
                            $countProjectData[] = $l;
                        }
                    }
                }
            }
        }

        //table8三公支出费用
        $threePublicObj = new AdminBudget();
        $threePublicObj = $threePublicObj->where('type', "1");
        $threePublicObj = $threePublicObj->where('economics_category_code', 'in', "30212,30913,31013,30231,30217");
        $threePublic = $threePublicObj
            ->field('economics_category_code,functional_category_code_class,functional_category_name_class name,economics_category_name_class,project_category_code,project_category_name,sum(total) total')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->group('economics_category_code_fund')
            ->orderRaw('economics_category_code_fund*1 ASC')->select()->toArray();
        $threePublic = array_combine(array_column($threePublic, 'economics_category_code'), array_column($threePublic, 'total'));
        $threePublic['total'] = array_sum($threePublic);
        //        var_dump($threePublicObj->getLastSql());

        //table9政府性基金预算支出情况表
        $countGovernmentClassObj = new AdminBudget();
        $countGovernmentClassObj = $countGovernmentClassObj->where('type', "1");
        $countGovernmentClassData = $countGovernmentClassObj
            ->field('functional_category_code_class,functional_category_name_class,economics_category_name_class,project_category_code,project_category_name,sum(total) total,sum(government_budgets) government_budgets_total,government_budgets,functional_category_name name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('government_budgets', '>', 0)
            ->group('functional_category_code_class')
            ->orderRaw('functional_category_code_class*1 ASC')->select()->toArray();
        $countGovernmentFundObj = new AdminBudget();
        $countGovernmentFundObj = $countGovernmentFundObj->where('type', "1");
        $countGovernmentFundData = $countGovernmentFundObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_name_fund,economics_category_name_class,project_category_code,project_category_name,sum(total) total,sum(government_budgets) government_budgets_total,government_budgets,functional_category_name name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('government_budgets', '>', 0)
            ->group('functional_category_code_class,functional_category_code_fund')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC')->select()->toArray();

        $countGovernmentItemObj = new AdminBudget();
        $countGovernmentItemObj = $countGovernmentItemObj->where('type', "1");
        $countGovernmentItemData = $countGovernmentItemObj
            ->field('functional_category_code_class,functional_category_code_fund,functional_category_code_item,functional_category_name_item,economics_category_name_class,project_name,project_category_code,project_category_name,sum(total) total,sum(government_budgets) government_budgets_total,government_budgets,functional_category_name name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('government_budgets', '>', 0)
            ->group('functional_category_code_class,functional_category_code_fund,functional_category_code_item')
            ->orderRaw('functional_category_code_class*1 ASC,functional_category_code_fund*1 ASC,functional_category_code_item*1 ASC')->select()->toArray();

        $countGovernmentData = [];
        $countGovernmentTotal = 0;
        $countGovernmentProjectTotal = 0;
        $countGovernmentBaseTotal = 0;
        foreach ($countGovernmentClassData as $v) {
            $countGovernmentData[] = $v;
            $countGovernmentTotal += $v['government_budgets_total'];
            if ($v['project_category_code'] == 1) {
                $countGovernmentBaseTotal += $v['government_budgets_total'];
            }
            if ($v['project_category_code'] == 2) {
                $countGovernmentProjectTotal += $v['government_budgets_total'];
            }
            foreach ($countGovernmentFundData as $j) {
                if ($j['functional_category_code_class'] == $v['functional_category_code_class']) {
                    $countGovernmentData[] = $j;
                    foreach ($countGovernmentItemData as $l) {
                        if ($l['functional_category_code_class'] == $v['functional_category_code_class'] && $l['functional_category_code_fund'] == $j['functional_category_code_fund']) {
                            $countGovernmentData[] = $l;
                        }
                    }
                }
            }
        }

        //机关运行经费
        $officeRunFundingObj = new AdminBudget();
        $officeRunFundingObj = $officeRunFundingObj->where('type', "1");
        $officeRunFunding = $officeRunFundingObj->field('economics_category_name')
            ->where(['institutions_code' => $ins_code, 'district_code' => $dis_code])
            ->where('economics_category_code_class', '302')
            ->where('fiscal_appropriation', '>', 0)
            ->group('economics_category_code,economics_category_name')->column('economics_category_name');
        array_unique($officeRunFunding);


        $projectObj = new AdminProject();
        // $projectList = $projectObj->where(['institutions_code'=>$ins_code,'district_code'=>$dis_code])->select()->toArray();
        $projectList = $projectObj->where(['uniq_code' => $code])->select()->toArray();

        //处理数据
        foreach ($countData as $k => $v) {
            $countData[$k] = $v / 10000;
        }
        foreach ($budgetData as $k => $v) {
            $budgetData[$k] = $v / 10000;
        }
        foreach ($countDetailData as $k => $v) {
            $v['total'] = $v['total'] / 10000;
            $v['income_total'] = $v['income_total'] / 10000;
            $v['general_public_budget_count'] = $v['general_public_budget_count'] / 10000;
            $v['government_budgets_count'] = $v['government_budgets_count'] / 10000;
            $v['government_capital_management_budget_count'] = $v['government_capital_management_budget_count'] / 10000;
            $v['special_financial_account_count'] = $v['special_financial_account_count'] / 10000;
            $v['utility_income_count'] = $v['utility_income_count'] / 10000;
            $v['institution_management_income_count'] = $v['institution_management_income_count'] / 10000;
            $v['institution_other_income_count'] = $v['institution_other_income_count'] / 10000;
            $countDetailData[$k] = $v;
        }
        foreach ($countProjectData as $k => $v) {
            $v['total'] = $v['total'] / 10000;
            $countProjectData[$k] = $v;
        }
        foreach ($economicsDetailData as $k => $v) {
            $v['total'] = $v['total'] / 10000;
            $economicsDetailData[$k] = $v;
        }

        foreach ($threePublic as $k => $v) {
            $threePublic[$k] = $v / 10000;
        }

        $listInfo['obj'] = $listObj;
        $listInfo['incomeCount'] = $countData;
        $listInfo['budgetCount'] = $budgetData;
        $listInfo['countDetailData'] = [
            'list' => $countDetailData,
            'general_public_budget_count_total' => $general_public_budget_count_total / 10000,
            'government_budgets_count_total' => $government_budgets_count_total / 10000,
            'government_capital_management_budget_count_total' => $government_capital_management_budget_count_total / 10000,
            'special_financial_account_count_total' => $special_financial_account_count_total / 10000,
            'utility_income_count_total' => $utility_income_count_total / 10000,
            'institution_management_income_count_total' => $institution_management_income_count_total / 10000,
            'institution_other_income_count_total' => $institution_other_income_count_total / 10000,
            'base_budget_total' => $base_budget_total / 10000,
            'project_budget_total' => $project_budget_total / 10000,
            'income_total' => $income_total / 10000,
            'budget_total' => $budget_total / 10000,
            'general_public_base_budget_total' => $general_public_base_budget_total / 10000,
            'general_public_project_budget_total' => $general_public_project_budget_total / 10000,
        ];
        $listInfo['countClassData'] = $countClassData;
        $listInfo['countFundData'] = $countFundData;
        $listInfo['countItemData'] = $countItemData;
        $listInfo['general_public_budget_data'] = $general_public_budget_data;
        $listInfo['economicsDetailData'] = [
            'list' => $economicsDetailData,
            'economicsDetailTotal' => $economicsDetailTotal / 10000,
            'economicsPeopleTotal' => $economicsPeopleTotal / 10000,
            'economicsPublicTotal' => $economicsPublicTotal / 10000
        ];
        $listInfo['countProjectData'] = [
            'list' => $countProjectData,
            'countProjectNameDetailData' => $countProjectNameDetailData
        ];
        $listInfo['threePublic'] = $threePublic;
        $listInfo['countGovernmentData'] = [
            'list' => $countGovernmentData,
            'countGovernmentTotal' => $countGovernmentTotal,
            'countGovernmentProjectTotal' => $countGovernmentProjectTotal,
            'countGovernmentBaseTotal' => $countGovernmentBaseTotal,
        ];
        $listInfo['officeRunFunding'] = $officeRunFunding;
        $listInfo['projectList'] = $projectList;

        return $listInfo;
    }
    public function download()
    {
        $code = $this->request->post('code', '');
        $file = $this->getDownload($code);
        return $file['file'];
    }
    public function batchDownload()
    {
        $code = $this->request->post('codes', '');
        $zip_name = root_path() . 'public/uploads/report.zip';
        $zipObj = new \ZipArchive();
        $zipObj->open($zip_name, \ZipArchive::CREATE);
        foreach ($code as $v) {
            $file =  $this->getDownload($v);
            $name = $file['name'];
            $zipObj->addFile($name, basename($name));
        }
        $zipObj->close();
        return download($zip_name);
    }
    public function getDownload($code)
    {
        $data = $this->getReportDetailData($code);
        $temp_name = root_path() . 'public/uploads/report.docx';   //模板文件的地址
        $tmp = new \PhpOffice\PhpWord\TemplateProcessor($temp_name); //打开模板

        //第一部分
        $tmp->setValue('institutions_name', $data['obj']['institutions_name']); //替换变量name
        $tmp->setValue('main_function', $data['obj']['main_function']); //替换变量main_function
        $tmp->setValue('office_data', implode('、', json_decode($data['obj']['office_data']))); //替换变量office_data
        if ($data['obj']['has_subordinate_unit']) {
            $tmp->cloneBlock('UNITBLOCK', 1);
            $tmp->cloneBlock('NOUNITBLOCK', 0);
            $tmp->setValue('subordinate_unit_data', implode('、', json_decode($data['obj']['subordinate_unit_data']))); //替换变量office_data
            $tmp->setValue('subordinate_unit_num', $data['obj']['subordinate_unit_num']); //替换变量office_data
            $tmp->setValue('administrative_unit_num', $data['obj']['administrative_unit_num']); //替换变量office_data
            $tmp->setValue('public_unit_num', $data['obj']['public_unit_num']); //替换变量office_data
        } else {
            $tmp->cloneBlock('NOUNITBLOCK', 1);
            $tmp->cloneBlock('UNITBLOCK', 0);
        }
        $tmp->setValue('staffing_num', $data['obj']['staffing_num']);
        $tmp->setValue('actual_num', $data['obj']['actual_num']);
        $staffing_data = json_decode($data['obj']['staffing_data'], true);
        $tmp->setValue('onJobCount', $staffing_data['onJob']['count']);
        $tmp->setValue('onJobChangeType', $staffing_data['onJob']['changeType'] == 1 ? '增加' : '减少');
        $tmp->setValue('onJobChangeNum', $staffing_data['onJob']['changeNum']);
        $tmp->setValue('retirementCount', $staffing_data['retirement']['count']);
        $tmp->setValue('retirementChangeType', $staffing_data['retirement']['changeType'] == 1 ? '增加' : '减少');
        $tmp->setValue('retirementChangeNum', $staffing_data['retirement']['changeNum']);
        $tmp->setValue('retireCount', $staffing_data['retire']['count']);
        $tmp->setValue('retireChangeType', $staffing_data['retire']['changeType'] == 1 ? '增加' : '减少');
        $tmp->setValue('retireChangeNum', $staffing_data['retire']['changeNum']);

        //第二部分
        //table1
        foreach ($data['incomeCount'] as $k => $v) {
            $tmp->setValue($k, $v);
        }
        $budget_code = [201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 219, 220, 221, 222, 223, 224, 227, 229, 230, 231, 232, 233, 234];
        foreach ($budget_code as $v) {
            if (isset($data['budgetCount'][$v])) {
                $tmp->setValue($v, $data['budgetCount'][$v]);
            } else {
                $tmp->setValue($v, '');
            }
        }
        $tmp->setValue('budget_total', $data['budgetCount']['budget_total']);

        //table2
        $rows = count($data['countDetailData']['list']); //总行数
        $tmp->cloneRow('table2_class', $rows); //复制行
        foreach ($data['countDetailData']['list'] as $k => $v) {
            $tmp->setValue('table2_class#' . ($k + 1), isset($v['functional_category_code_class']) ? $v['functional_category_code_class'] : '');
            $tmp->setValue('table2_fund#' . ($k + 1), isset($v['functional_category_code_fund']) ? $v['functional_category_code_fund'] : '');
            $tmp->setValue('table2_item#' . ($k + 1), isset($v['functional_category_code_item']) ? $v['functional_category_code_item'] : '');
            $tmp->setValue('table2_name#' . ($k + 1), $v['name']);
            $tmp->setValue('table2_income_total#' . ($k + 1), $v['income_total']);
            $tmp->setValue('table2_general_public_budget_count#' . ($k + 1), isset($v['general_public_budget_count']) ? $v['general_public_budget_count'] : '');
            $tmp->setValue('table2_government_budgets_count#' . ($k + 1), isset($v['government_budgets_count']) ? $v['government_budgets_count'] : '');
            $tmp->setValue('table2_government_capital_management_budget_count#' . ($k + 1), isset($v['government_capital_management_budget_count']) ? $v['government_capital_management_budget_count'] : '');
            $tmp->setValue('table2_special_financial_account_count#' . ($k + 1), isset($v['special_financial_account_count']) ? $v['special_financial_account_count'] : '');
            $tmp->setValue('table2_utility_income_count#' . ($k + 1), isset($v['utility_income_count']) ? $v['utility_income_count'] : '');
            $tmp->setValue('table2_institution_management_income_count#' . ($k + 1), isset($v['institution_management_income_count']) ? $v['institution_management_income_count'] : '');
            $tmp->setValue('table2_institution_other_income_count#' . ($k + 1), isset($v['institution_other_income_count']) ? $v['institution_other_income_count'] : '');
        }
        $tmp->setValue('general_public_budget_count_total', isset($data['countDetailData']['general_public_budget_count_total']) ? $data['countDetailData']['general_public_budget_count_total'] : '');
        $tmp->setValue('government_budgets_count_total', isset($data['countDetailData']['government_budgets_count_total']) ? $data['countDetailData']['government_budgets_count_total'] : '');
        $tmp->setValue('government_capital_management_budget_count_total', isset($data['countDetailData']['government_capital_management_budget_count_total']) ? $data['countDetailData']['government_capital_management_budget_count_total'] : '');
        $tmp->setValue('special_financial_account_count_total', isset($data['countDetailData']['special_financial_account_count_total']) ? $data['countDetailData']['special_financial_account_count_total'] : '');
        $tmp->setValue('utility_income_count_total', isset($data['countDetailData']['utility_income_count_total']) ? $data['countDetailData']['utility_income_count_total'] : '');
        $tmp->setValue('institution_management_income_count_total', isset($data['countDetailData']['institution_management_income_count_total']) ? $data['countDetailData']['institution_management_income_count_total'] : '');
        $tmp->setValue('institution_other_income_count_total', isset($data['countDetailData']['institution_other_income_count_total']) ? $data['countDetailData']['institution_other_income_count_total'] : '');

        //table3
        $tmp->cloneRow('table3_class', $rows); //复制行
        foreach ($data['countDetailData']['list'] as $k => $v) {
            $tmp->setValue('table3_class#' . ($k + 1), isset($v['functional_category_code_class']) ? $v['functional_category_code_class'] : '');
            $tmp->setValue('table3_fund#' . ($k + 1), isset($v['functional_category_code_fund']) ? $v['functional_category_code_fund'] : '');
            $tmp->setValue('table3_item#' . ($k + 1), isset($v['functional_category_code_item']) ? $v['functional_category_code_item'] : '');
            $tmp->setValue('table3_name#' . ($k + 1), $v['name']);
            $tmp->setValue('table3_total#' . ($k + 1), $v['total']);
            $tmp->setValue('table3_base_total#' . ($k + 1), $v['project_category_code'] == 1 ? $v['total'] : '');
            $tmp->setValue('table3_project_total#' . ($k + 1), $v['project_category_code'] == 2 ? $v['total'] : '');
        }
        $tmp->setValue('budget_total', isset($data['countDetailData']['budget_total']) ? $data['countDetailData']['budget_total'] : '');
        $tmp->setValue('base_budget_total', isset($data['countDetailData']['base_budget_total']) ? $data['countDetailData']['base_budget_total'] : '');
        $tmp->setValue('project_budget_total', isset($data['countDetailData']['project_budget_total']) ? $data['countDetailData']['project_budget_total'] : '');

        //table4
        $countClassData = array_combine(array_column($data['countClassData'], 'functional_category_code_class'), $data['countClassData']);
        foreach ($budget_code as $v) {
            if (isset($countClassData[$v])) {
                $tmp->setValue($v . 'total', $countClassData[$v]['total']);
                $tmp->setValue($v . 'general_public_budget_total', $countClassData[$v]['general_public_budget_count']);
                $tmp->setValue($v . 'government_budgets_total', $countClassData[$v]['government_budgets_count']);
                $tmp->setValue($v . 'government_capital_management_budget_total', $countClassData[$v]['government_capital_management_budget_count']);
            } else {
                $tmp->setValue($v . 'total', '');
                $tmp->setValue($v . 'general_public_budget_total', '');
                $tmp->setValue($v . 'government_budgets_total', '');
                $tmp->setValue($v . 'government_capital_management_budget_total', '');
            }
        }
        //table5
        $tmp->cloneRow('table5_class', $rows); //复制行
        foreach ($data['countDetailData']['list'] as $k => $v) {
            $tmp->setValue('table5_class#' . ($k + 1), isset($v['functional_category_code_class']) ? $v['functional_category_code_class'] : '');
            $tmp->setValue('table5_fund#' . ($k + 1), isset($v['functional_category_code_fund']) ? $v['functional_category_code_fund'] : '');
            $tmp->setValue('table5_item#' . ($k + 1), isset($v['functional_category_code_item']) ? $v['functional_category_code_item'] : '');
            $tmp->setValue('table5_name#' . ($k + 1), $v['name']);
            $tmp->setValue('table5_total#' . ($k + 1), $v['total']);
            $tmp->setValue('table5_base_total#' . ($k + 1), $v['project_category_code'] == 1 ? $v['total'] : '');
            $tmp->setValue('table5_project_total#' . ($k + 1), $v['project_category_code'] == 2 ? $v['total'] : '');
        }

        //table6
        $tmp->cloneRow('table6_class', count($data['economicsDetailData']['list'])); //复制行
        foreach ($data['economicsDetailData']['list'] as $k => $v) {
            $tmp->setValue('table6_class#' . ($k + 1), isset($v['economics_category_code_class']) ? $v['economics_category_code_class'] : '');
            $tmp->setValue('table6_fund#' . ($k + 1), isset($v['economics_category_code_fund']) ? $v['economics_category_code_fund'] : '');
            $tmp->setValue('table6_name#' . ($k + 1), $v['name']);
            $tmp->setValue('table6_total#' . ($k + 1), $v['total']);
            $tmp->setValue('table6_people_total#' . ($k + 1), $v['economics_category_code_class'] == 301 || $v['economics_category_code_class'] == 303 ? $v['total'] : '');
            $tmp->setValue('table6_public_total#' . ($k + 1), $v['economics_category_code_class'] == 302 ? $v['total'] : '');
        }

        $tmp->setValue('economicsDetailTotal', $data['economicsDetailData']['economicsDetailTotal']);
        $tmp->setValue('economicsPeopleTotal', $data['economicsDetailData']['economicsPeopleTotal']);
        $tmp->setValue('economicsPublicTotal', $data['economicsDetailData']['economicsPublicTotal']);

        //table8
        $tmp->setValue('threePublic_total', $data['threePublic']['total']);
        $tmp->setValue('threePublic_trip_total', isset($data['threePublic']['30212']) ? floatval($data['threePublic']['30212']) : '');
        $tmp->setValue('threePublic_car_total', isset($data['threePublic']['30913']) ? floatval($data['threePublic']['30913']) : 0 + isset($data['threePublic']['31013']) ? floatval($data['threePublic']['31013']) : 0 + isset($data['threePublic']['30231']) ? floatval($data['threePublic']['30231']) : 0);
        $tmp->setValue('threePublic_buy_car_total', isset($data['threePublic']['30913']) ? floatval($data['threePublic']['30913']) : 0 + isset($data['threePublic']['31013']) ? floatval($data['threePublic']['31013']) : 0);
        $tmp->setValue('threePublic_use_car_total', isset($data['threePublic']['30231']) ? floatval($data['threePublic']['30231']) : '');
        $tmp->setValue('threePublic_jiedai_total', isset($data['threePublic']['30217']) ? floatval($data['threePublic']['30217']) : '');


        //table9
        $tmp->cloneRow('table9_class', count($data['countGovernmentData']['list'])); //复制行
        foreach ($data['countGovernmentData']['list'] as $k => $v) {
            $tmp->setValue('table9_class#' . ($k + 1), isset($v['functional_category_code_class']) ? $v['functional_category_code_class'] : '');
            $tmp->setValue('table9_fund#' . ($k + 1), isset($v['functional_category_code_fund']) ? $v['functional_category_code_fund'] : '');
            $tmp->setValue('table9_item#' . ($k + 1), isset($v['functional_category_code_item']) ? $v['functional_category_code_item'] : '');
            $tmp->setValue('table9_name#' . ($k + 1), $v['name']);
            $tmp->setValue('table9_total#' . ($k + 1), $v['total']);
            $tmp->setValue('table9_base_total#' . ($k + 1), $v['project_category_code'] == 1 ? $v['total'] : '');
            $tmp->setValue('table9_project_total#' . ($k + 1), $v['project_category_code'] == 2 ? $v['total'] : '');
        }
        $tmp->setValue('table9_budget_total', $data['countGovernmentData']['countGovernmentTotal'] ? $data['countGovernmentData']['countGovernmentTotal'] : '');
        $tmp->setValue('table9_base_budget_total', $data['countGovernmentData']['countGovernmentBaseTotal'] ? $data['countGovernmentData']['countGovernmentBaseTotal'] : '');
        $tmp->setValue('table9_project_budget_total', $data['countGovernmentData']['countGovernmentProjectTotal'] ? $data['countGovernmentData']['countGovernmentProjectTotal'] : '');

        //第三部分
        $part_three_data = $this->getPartThreeData($code);
        //        var_dump($part_three_data);
        //3-1
        $tmp->setValue('shbzjyzc', round($this->sabs($part_three_data['thisyear']['shbzjyzc']),2));
        $tmp->setValue('wsjkzc', round($this->sabs($part_three_data['thisyear']['wsjkzc']),2));
        $tmp->setValue('jnhbzc', round($this->sabs($part_three_data['thisyear']['jnhbzc']),2));
        $tmp->setValue('cxsqzc', round($this->sabs($part_three_data['thisyear']['cxsqzc']),2));
        $tmp->setValue('nlszc', round($this->sabs($part_three_data['thisyear']['nlszc']),2));
        $tmp->setValue('zrzyhyqxzc', round($this->sabs($part_three_data['thisyear']['zrzyhyqxzc']),2));
        $tmp->setValue('zfbzzc', round($this->sabs($part_three_data['thisyear']['zfbzzc']),2));
        $tmp->setValue('zhfzzc', round($this->sabs($part_three_data['thisyear']['zhfzzc']),2));
        // 3-2
        // 一般公共预算
        $tmp->setValue('z1', round($this->sabs($part_three_data['thisyear']['general_public_budget']),2));
        $tmp->setValue('z2', round($this->sabs($part_three_data['calc']['v1']),2));
        $tmp->setValue('z3', round($this->sabs($part_three_data['calc']['v2']),2));
        $tmp->setValue('m3', $this->judge($part_three_data['calc']['v2']));
        $tmp->setValue('z4', round($this->sabs($part_three_data['calc']['v3']),2));
        // 政府性基金
        $tmp->setValue('z5', round($this->sabs($part_three_data['thisyear']['government_budgets']),2));
        $tmp->setValue('z6', round($this->sabs($part_three_data['calc']['v4']),2));
        $tmp->setValue('z7', round($this->sabs($part_three_data['calc']['v5']),2));
        $tmp->setValue('m7', $this->judge($part_three_data['calc']['v5']));
        $tmp->setValue('z8', round($this->sabs($part_three_data['calc']['v6']),2));
        // 其他资金
        $tmp->setValue('z9', round($this->sabs($part_three_data['thisyear']['institution_other_income']),2));
        $tmp->setValue('z10', round($this->sabs($part_three_data['calc']['v7']),2));
        $tmp->setValue('z11', round($this->sabs($part_three_data['calc']['v8']),2));
        $tmp->setValue('m11', $this->judge($part_three_data['calc']['v8']));
        $tmp->setValue('z12', round($this->sabs($part_three_data['calc']['v9']),2));
        // 3-3
        // total
        $tmp->setValue('ztotal', round($part_three_data['thisyear']['total'], 2));
        // 基本支出
        $tmp->setValue('z13', round($this->sabs($part_three_data['thisyear']['jbzc']),2));
        $tmp->setValue('z14', round($this->sabs($part_three_data['calc']['v10']),2));
        $tmp->setValue('z15', round($this->sabs($part_three_data['calc']['v11']),2));
        $tmp->setValue('m15', $this->judge($part_three_data['calc']['v11']));
        $tmp->setValue('z16', round($this->sabs($part_three_data['calc']['v12']),2));
        // 项目支出
        $tmp->setValue('z17', round($this->sabs($part_three_data['thisyear']['xmzc']),2));
        $tmp->setValue('z18', round($this->sabs($part_three_data['calc']['v13']),2));
        $tmp->setValue('z19', round($this->sabs($part_three_data['calc']['v14']),2));
        $tmp->setValue('m19', $this->judge($part_three_data['calc']['v14']));
        $tmp->setValue('z20', round($this->sabs($part_three_data['calc']['v15']),2));
        // 3-4
        // 财政拨款
        $tmp->setValue('z21', round($part_three_data['thisyear']['fiscal_appropriation'], 2));
        // 收入预算
        $tmp->setValue('z22', round($part_three_data['thisyear']['general_public_budget'], 2));
        $tmp->setValue('z23', round($part_three_data['thisyear']['government_budgets'], 2));
        // 一般公共预算支出
        $tmp->setValue('z24', round($part_three_data['thisyear']['shbzjyzc'], 2));
        $tmp->setValue('z25', round($part_three_data['thisyear']['wsjkzc'], 2));
        $tmp->setValue('z26', round($part_three_data['thisyear']['jnhbzc'], 2));
        $tmp->setValue('z27', round($part_three_data['thisyear']['nlszc'], 2));
        $tmp->setValue('z28', round($part_three_data['thisyear']['zrzyhyqxzc'], 2));
        $tmp->setValue('z29', round($part_three_data['thisyear']['zfbzzc'], 2));
        $tmp->setValue('z30', round($part_three_data['thisyear']['zhfzzc'], 2));
        // 政府性基金预算拨款
        $tmp->setValue('z31', round($part_three_data['thisyear']['government_budgets'], 2));
        // 一般公共预算拨款合计
        $tmp->setValue('z32', round($part_three_data['thisyear']['general_public_budget'], 2));
        // 基本支出
        $tmp->setValue('z33', round($part_three_data['thisyear']['jbzc'], 2));
        $tmp->setValue('z34',$this->sabs( round($part_three_data['calc']['v11']),2));
        $tmp->setValue('m34', $this->judge($part_three_data['calc']['v11']));
        $tmp->setValue('z35', round($this->sabs($part_three_data['calc']['v12']),2));
        // 项目支出
        $tmp->setValue('z36', round($part_three_data['thisyear']['xmzc'], 2));
        $tmp->setValue('z37', round($this->sabs($part_three_data['calc']['v14']),2));
        $tmp->setValue('m37', $this->judge($part_three_data['calc']['v14']));
        $tmp->setValue('z38', round($this->sabs($part_three_data['calc']['v15']),2));
        // 一般公共预算
        $tmp->setValue('z39', round($part_three_data['thisyear']['shbzjyzc'], 2));
        $tmp->setValue('z40', round($this->sabs($part_three_data['calc']['v19']),2));

        $tmp->setValue('z41', round($part_three_data['thisyear']['wsjkzc'], 2));
        $tmp->setValue('z42', round($this->sabs($part_three_data['calc']['v20']),2));

        $tmp->setValue('z43', round($part_three_data['thisyear']['jnhbzc'], 2));
        $tmp->setValue('z44', round($this->sabs($part_three_data['calc']['v21']),2));

        $tmp->setValue('z45', round($part_three_data['thisyear']['nlszc'], 2));
        $tmp->setValue('z46', round($this->sabs($part_three_data['calc']['v22']),2));

        $tmp->setValue('z47', round($part_three_data['thisyear']['zrzyhyqxzc'], 2));
        $tmp->setValue('z48', round($this->sabs($part_three_data['calc']['v23']),2));

        $tmp->setValue('z49', round($part_three_data['thisyear']['zfbzzc'], 2));
        $tmp->setValue('z50', round($this->sabs($part_three_data['calc']['v24']),2));

        $tmp->setValue('z51', round($part_three_data['thisyear']['zhfzzc'], 2));
        $tmp->setValue('z52', round($this->sabs($part_three_data['calc']['v25']),2));

        // 社会保障就业 离退休
        $tmp->setValue('z53', round($part_three_data['thisyear']['a1'], 2));
        $tmp->setValue('z54', round($this->sabs($part_three_data['calc']['v26']),2));
        $tmp->setValue('z55', round($this->sabs($part_three_data['calc']['v27']),2));
        // 社会保障就业 养老保险
        $tmp->setValue('z56', round($part_three_data['thisyear']['a2'], 2));
        $tmp->setValue('z57', round($this->sabs($part_three_data['calc']['v28']),2));
        $tmp->setValue('m57', $this->judge($part_three_data['calc']['v28']));
        $tmp->setValue('z58', round($this->sabs($part_three_data['calc']['v29']),2));
        // 社会保障就业 单位医疗
        $tmp->setValue('z59', round($part_three_data['thisyear']['a3'], 2));
        $tmp->setValue('z60', round($this->sabs($part_three_data['calc']['v30']),2));
        $tmp->setValue('m60', $this->judge($part_three_data['calc']['v30']));
        $tmp->setValue('z61', round($this->sabs($part_three_data['calc']['v31']),2));
        // 社会保障就业 医疗补助
        $tmp->setValue('z62', round($part_three_data['thisyear']['a4'], 2));
        $tmp->setValue('z63', round($this->sabs($part_three_data['calc']['v32']),2));
        $tmp->setValue('m63', $this->judge($part_three_data['calc']['v32']));
        $tmp->setValue('z64', round($this->sabs($part_three_data['calc']['v33']),2));

        // 5
        $tmp->setValue('z65', round($part_three_data['thisyear']['a5'], 2));
        $tmp->setValue('z66', round($this->sabs($part_three_data['calc']['v34']),2));
        $tmp->setValue('m66', $this->judge($part_three_data['calc']['v34']));
        $tmp->setValue('z67', round($this->sabs($part_three_data['calc']['v35']),2));
        // 6
        $tmp->setValue('z68', round($part_three_data['thisyear']['a6'], 2));
        $tmp->setValue('z69', round($this->sabs($part_three_data['calc']['v36']),2));
        $tmp->setValue('m69', $this->judge($part_three_data['calc']['v36']));
        $tmp->setValue('z70', round($this->sabs($part_three_data['calc']['v37']),2));
        // 7
        $tmp->setValue('z71', round($part_three_data['thisyear']['a7'], 2));
        $tmp->setValue('z72', round($this->sabs($part_three_data['calc']['v38']),2));
        $tmp->setValue('m72', $this->judge($part_three_data['calc']['v38']));
        $tmp->setValue('z73', round($this->sabs($part_three_data['calc']['v39']),2));
        // 8
        $tmp->setValue('z74', round($part_three_data['thisyear']['a8'], 2));
        $tmp->setValue('z75', round($this->sabs($part_three_data['calc']['v40']),2));
        $tmp->setValue('m75', $this->judge($part_three_data['calc']['v40']));
        $tmp->setValue('z76', round($this->sabs($part_three_data['calc']['v41']),2));
        // 9
        $tmp->setValue('z77', round($part_three_data['thisyear']['a9'], 2));
        $tmp->setValue('z78', round($this->sabs($part_three_data['calc']['v42']),2));
        $tmp->setValue('m78', $this->judge($part_three_data['calc']['v42']));
        $tmp->setValue('z79', round($this->sabs($part_three_data['calc']['v43']),2));
        // 10
        $tmp->setValue('z80', round($part_three_data['thisyear']['a10'], 2));
        $tmp->setValue('z81', round($this->sabs($part_three_data['calc']['v44']),2));
        $tmp->setValue('m81', $this->judge($part_three_data['calc']['v44']));
        $tmp->setValue('z82', round($this->sabs($part_three_data['calc']['v45']),2));
        // 11
        $tmp->setValue('z83', round($part_three_data['thisyear']['a11'], 2));
        $tmp->setValue('z84', round($part_three_data['calc']['v46'], 2));
        $tmp->setValue('m84', $this->judge($part_three_data['calc']['v46']));
        $tmp->setValue('z85', round($part_three_data['calc']['v47'], 2));
        // 12
        $tmp->setValue('z86', round($part_three_data['thisyear']['a12'], 2));
        $tmp->setValue('z87', round($this->sabs($part_three_data['calc']['v48']),2));
        $tmp->setValue('m87', $this->judge($part_three_data['calc']['v48']));
        $tmp->setValue('z88', round($this->sabs($part_three_data['calc']['v49']),2));
        // 13
        $tmp->setValue('z89', round($part_three_data['thisyear']['a13'], 2));
        $tmp->setValue('z90', round($this->sabs($part_three_data['calc']['v50']),2));
        $tmp->setValue('m90', $this->judge($part_three_data['calc']['v50']));
        $tmp->setValue('z91', round($this->sabs($part_three_data['calc']['v51']),2));
        // 14
        $tmp->setValue('z92', round($part_three_data['thisyear']['a14'], 2));
        $tmp->setValue('z93', round($this->sabs($part_three_data['calc']['v52']),2));
        $tmp->setValue('m93', $this->judge($part_three_data['calc']['v52']));
        $tmp->setValue('z94', round($this->sabs($part_three_data['calc']['v52']),2));
        // 3-6
        $tmp->setValue('z95', round($part_three_data['thisyear']['jbzc'], 2));
        // 人员经费
        $tmp->setValue('z96', round($part_three_data['thisyear']['a15'], 2));
        // 公用经费
        $tmp->setValue('z97', round($part_three_data['thisyear']['a16'], 2));

        //3-7
        //项目列表
        //        var_dump(count($data['projectList']));exit;
        $tmp->cloneBlock('project_block', count($data['projectList'])); //复制块
        $people_block = 0;
        foreach ($data['projectList'] as $k => $v) {
            $tmp->setValue('project_name#' . ($k + 1), ($k + 1) . '项目名称：' . $v['project_name']);
            $tmp->setValue('policy_basis#' . ($k + 1), isset($v['policy_basis']) ? $v['policy_basis'] : '');
            $tmp->setValue('budget_size#' . ($k + 1), isset($v['budget_size']) ? $v['budget_size'] : '');
            $tmp->setValue('responsible_unit#' . ($k + 1), $v['responsible_unit']);
            $tmp->setValue('fund_distribution#' . ($k + 1), $v['fund_distribution']);
            $tmp->setValue('fund_execution_tiem#' . ($k + 1), $v['fund_execution_tiem']);
            if ($v['type'] != 2) {
                $tmp->cloneBlock('people_block', 0); //删除块
            } else {
                $tmp->cloneBlock('people_block', 1); //显示块
                $tmp->setValue('fund_source#' . ($people_block + 1), $v['fund_source']);
                $tmp->setValue('subsidies_number#' . ($people_block + 1), $v['subsidies_number']);
                $tmp->setValue('subsidies_standard#' . ($people_block + 1), $v['subsidies_standard']);
                $tmp->setValue('subsidies_range#' . ($people_block + 1), $v['subsidies_range']);
                $tmp->setValue('subsidies_way#' . ($people_block + 1), $v['subsidies_way']);
                $tmp->setValue('distribution_program#' . ($people_block + 1), $v['distribution_program']);
                $tmp->setValue('beneficiary_group#' . ($people_block + 1), $v['beneficiary_group']);
                $people_block++;
            }
        }

        // 3-8
        // 三公
        $tmp->setValue('z98', round($part_three_data['thisyear']['sgtotal'], 2));
        $tmp->setValue('z99', round($part_three_data['thisyear']['ygcg'], 2));
        $tmp->setValue('z100', round($part_three_data['thisyear']['gwycgz'], 2));
        $tmp->setValue('z101', round($part_three_data['thisyear']['gwycwh'], 2));
        $tmp->setValue('z102', round($part_three_data['thisyear']['gwjdf'], 2));

        $tmp->setValue('z103', round($part_three_data['calc']['v54'], 2));
        $tmp->setValue('m103', $this->sabs($this->judge($part_three_data['calc']['v54'])));
        $tmp->setValue('z104', round($this->sabs($part_three_data['calc']['v55']),2));
        $tmp->setValue('z105', round($this->sabs($part_three_data['calc']['v56']),2));
        $tmp->setValue('m105', $this->judge($part_three_data['calc']['v56']));
        $tmp->setValue('z106', round($this->sabs($part_three_data['calc']['v57']),2));

        $tmp->setValue('z107', round($part_three_data['thisyear']['gwycgz'], 2));
        $tmp->setValue('m107', $this->judge($part_three_data['thisyear']['gwycgz']));
        $tmp->setValue('z108', round($this->sabs($part_three_data['calc']['v62']),2));

        $tmp->setValue('z109', round($part_three_data['thisyear']['gwycwh'], 2));
        $tmp->setValue('m109', $this->judge($part_three_data['thisyear']['gwycwh']));
        $tmp->setValue('z110', round($this->sabs($part_three_data['calc']['v64']),2));

        $tmp->setValue('z111', round($part_three_data['thisyear']['gwjdf'], 2));
        $tmp->setValue('m111', $this->judge($part_three_data['thisyear']['gwjdf']));
        $tmp->setValue('z112', round($this->sabs($part_three_data['calc']['v63']),2));

        // 3-9
        $tmp->setValue('z113', round($part_three_data['thisyear']['government_budgets'], 2));
        $tmp->setValue('z114', round($this->sabs($part_three_data['calc']['v5']),2));
        $tmp->setValue('z115', round($this->sabs($part_three_data['calc']['v6']),2));

        $tmp->setValue('z116', round($part_three_data['thisyear']['a17'], 2));
        $tmp->setValue('z117', round($this->sabs($part_three_data['calc']['v58']),2));
        $tmp->setValue('m111', $this->judge($part_three_data['calc']['v58']));
        $tmp->setValue('z118', round($this->sabs($part_three_data['calc']['v59']),2));

        // 3-10
        $tmp->setValue('z119', round($part_three_data['thisyear']['jgyxjf'], 2));
        $tmp->setValue('z120', round($this->sabs($part_three_data['calc']['v60']),2));
        $tmp->setValue('m120', $this->judge($part_three_data['calc']['v60']));
        $tmp->setValue('z121', round($this->sabs($part_three_data['calc']['v61']),2));
        // 34 37 69 72 78 84 87 90 103 109 120
        $tmp->setValue('u4', $this->judge2($part_three_data['calc']['v3']));
        $tmp->setValue('u35', $this->judge2($part_three_data['calc']['v12']));
        $tmp->setValue('u38', $this->judge2($part_three_data['calc']['v15']));
        $tmp->setValue('u70', $this->judge2($part_three_data['calc']['v37']));
        $tmp->setValue('u73', $this->judge2($part_three_data['calc']['v39']));
        $tmp->setValue('u79', $this->judge2($part_three_data['calc']['v43']));
        $tmp->setValue('u85', $this->judge2($part_three_data['calc']['v47']));
        $tmp->setValue('u88', $this->judge2($part_three_data['calc']['v49']));
        $tmp->setValue('u91', $this->judge2($part_three_data['calc']['v51']));
        $tmp->setValue('u104', $this->judge2($part_three_data['calc']['v55']));
        $tmp->setValue('u110', $this->judge2($part_three_data['calc']['v64']));
        $tmp->setValue('u121', $this->judge2($part_three_data['calc']['v61']));

        $tmp->setValue('u8', $this->judge2($part_three_data['calc']['v6']));
        $tmp->setValue('u12', $this->judge2($part_three_data['calc']['v9']));
        $tmp->setValue('u16', $this->judge2($part_three_data['calc']['v12']));
        $tmp->setValue('u20', $this->judge2($part_three_data['calc']['v15']));
        $tmp->setValue('u55', $this->judge2($part_three_data['calc']['v27']));
        $tmp->setValue('u58', $this->judge2($part_three_data['calc']['v29']));
        $tmp->setValue('u61', $this->judge2($part_three_data['calc']['v31']));
        $tmp->setValue('u64', $this->judge2($part_three_data['calc']['v33']));
        $tmp->setValue('u67', $this->judge2($part_three_data['calc']['v35']));
        $tmp->setValue('u76', $this->judge2($part_three_data['calc']['v41']));
        $tmp->setValue('u82', $this->judge2($part_three_data['calc']['v45']));
        $tmp->setValue('u94', $this->judge2($part_three_data['calc']['v52']));
        $tmp->setValue('u106', $this->judge2($part_three_data['calc']['v57']));
        $tmp->setValue('u108', $this->judge2($part_three_data['calc']['v62']));
        $tmp->setValue('u112', $this->judge2($part_three_data['calc']['v63']));
        $tmp->setValue('u115', $this->judge2($part_three_data['calc']['v6']));
        $tmp->setValue('u118', $this->judge2($part_three_data['calc']['v59']));


        $file_name = root_path() . 'public/uploads/' . date('Y') . '年度' . $data['obj']['district_name'] . $data['obj']['institutions_name'] . '单位预算公开.docx';
        $tmp->saveAs($file_name); //另存为

        return ['file' => download($file_name), 'name' => $file_name];
    }

    public function getPartThreeData($code)
    {
        $dis_code = substr($code, 0, 6);
        $ins_code = substr($code, 6, 6);
        $thisyears = Db::query("select * from admin_budget where type = 1 and institutions_code = :code1 and district_code = :code2", ['code1' => $ins_code, 'code2' => $dis_code]);
        $lastyears = Db::query("select * from admin_budget where type = 2 and institutions_code = :code1 and district_code = :code2", ['code1' => $ins_code, 'code2' => $dis_code]);

        $thisyear = [];
        $lastyear = [];
        $calc = [];

        // 第三部分

        // 一般公共预算基本支出
        // 人员经费
        $ryarr = [30101, 30102, 30102, 30107, 30108, 30110, 30111, 30112, 30113, 30199, 30302, 30305, 30309, 30399];
        // 公用经费
        $gyarr = [30201, 30208, 30211, 30231];


        $total = 0;
        $general_public_budget = 0;
        $government_budgets = 0;
        $institution_other_income = 0;
        $jbzc = 0; // 基本支出
        $xmzc = 0; // 项目支出
        $fiscal_appropriation = 0;
        $shbzjyzc = 0;
        $wsjkzc = 0;
        $jnhbzc = 0;
        $cxsqzc = 0;
        $nlszc = 0;
        $zrzyhyqxzc = 0;
        $zfbzzc = 0;
        $zhfzzc = 0;
        $a1 = 0;
        $a2 = 0;
        $a3 = 0;
        $a4 = 0;
        $a5 = 0;
        $a6 = 0;
        $a7 = 0;
        $a8 = 0;
        $a9 = 0;
        $a10 = 0;
        $a11 = 0;
        $a12 = 0;
        $a13 = 0;
        $a14 = 0;
        $a15 = 0;
        $a16 = 0;
        $ygcg = 0;
        $gwycgz = 0;
        $gwycwh = 0;
        $gwjdf = 0;
        $a17 = 0;
        $jgyxjf = 0;
        $project_count = 0;
        $project_payment = 0;

        foreach ($thisyears as $item) {
            // if ($item['type'] == 2) continue;
            $total += $item['total'];
            $general_public_budget += $item['general_public_budget'];
            $government_budgets += $item['government_budgets'];
            $institution_other_income += $item['institution_other_income'];
            $fiscal_appropriation += $item['fiscal_appropriation'];
            $jbzc += $item['project_category_name'] == "基本支出" ? $item['total'] : 0;
            $xmzc += $item['project_category_name'] == "项目支出" ? $item['total'] : 0;
            $shbzjyzc += $item['functional_category_name_class'] == "社会保障和就业支出" ? $item['total'] : 0;
            $wsjkzc += $item['functional_category_name_class'] == "卫生健康支出" ? $item['total'] : 0;
            $jnhbzc += $item['functional_category_name_class'] == "节能环保支出" ? $item['total'] : 0;
            $cxsqzc += $item['functional_category_name_class'] == "城乡社区支出" ? $item['total'] : 0;
            $nlszc += $item['functional_category_name_class'] == "农林水支出" ? $item['total'] : 0;
            $zrzyhyqxzc += $item['functional_category_name_class'] == "自然资源海洋气象等支出" ? $item['total'] : 0;
            $zfbzzc += $item['functional_category_name_class'] == "住房保障支出" ? $item['total'] : 0;
            $zhfzzc += $item['functional_category_name_class'] == "灾害防治及应急管理支出" ? $item['total'] : 0;

            if ($item['functional_category_code_class'] == 208 && $item['functional_category_code_fund'] == 5) {
                if ($item['functional_category_code_item'] == 1) {
                    $a1 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 5) {
                    $a2 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 210 && $item['functional_category_code_fund'] == 11) {
                if ($item['functional_category_code_item'] == 1) {
                    $a3 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 3) {
                    $a4 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 211 && $item['functional_category_code_fund'] == 4) {
                if ($item['functional_category_code_item'] == 1) {
                    $a5 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 211 && $item['functional_category_code_fund'] == 6) {
                if ($item['functional_category_code_item'] == 99) {
                    $a6 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 213 && $item['functional_category_code_fund'] == 2) {
                if ($item['functional_category_code_item'] == 5) {
                    $a7 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 99) {
                    $a8 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 220 && $item['functional_category_code_fund'] == 1) {
                if ($item['functional_category_code_item'] == 1) {
                    $a9 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 6) {
                    $a10 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 50) {
                    $a11 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 99) {
                    $a12 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 221 && $item['functional_category_code_fund'] == 2) {
                if ($item['functional_category_code_item'] == 1) {
                    $a13 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 224 && $item['functional_category_code_fund'] == 1) {
                if ($item['functional_category_code_item'] == 4) {
                    $a14 += $item['general_public_budget'];
                }
            }
            if (in_array($item['economics_category_code'], $ryarr)) {
                $a15 += $item['general_public_budget'];
            }
            if (in_array($item['economics_category_code'], $gyarr)) {
                $a16 += $item['general_public_budget'];
            }
            if ($item['economics_category_code'] == "30212") {
                $ygcg += $item['general_public_budget'];
            }
            if (in_array($item['economics_category_code'], [30913, 31013])) {
                $gwycgz += $item['general_public_budget'];
            }
            if ($item['economics_category_code'] == "30231") {
                $gwycwh += $item['general_public_budget'];
            }
            if ($item['economics_category_code'] == "30217") {
                $gwjdf += $item['general_public_budget'];
            }
            if ($item['functional_category_code_class'] == 212 && $item['functional_category_code_fund'] == 8) {
                if ($item['functional_category_code_item'] == 16) {
                    $a17 += $item['general_public_budget'];
                }
            }
            if ($item['economics_category_code_class'] == "302") {
                $jgyxjf += $item['fiscal_appropriation'];
            }
            if ($item['project_category_code'] == "2") {
                $project_count += 1;
                $project_payment += $item['fiscal_appropriation'];
            }
        }
        $thisyear['total'] = $total;
        $thisyear['general_public_budget'] = $general_public_budget;
        $thisyear['government_budgets'] = $government_budgets;
        $thisyear['institution_other_income'] = $institution_other_income;
        $thisyear['fiscal_appropriation'] = $fiscal_appropriation;
        $thisyear['jbzc'] = $jbzc;
        $thisyear['xmzc'] = $xmzc;
        $thisyear['shbzjyzc'] = $shbzjyzc;
        $thisyear['wsjkzc'] = $wsjkzc;
        $thisyear['jnhbzc'] = $jnhbzc;
        $thisyear['cxsqzc'] = $cxsqzc;
        $thisyear['nlszc'] = $nlszc;
        $thisyear['zrzyhyqxzc'] = $zrzyhyqxzc;
        $thisyear['zfbzzc'] = $zfbzzc;
        $thisyear['zhfzzc'] = $zhfzzc;
        $thisyear['a1'] = $a1;
        $thisyear['a2'] = $a2;
        $thisyear['a3'] = $a3;
        $thisyear['a4'] = $a4;
        $thisyear['a5'] = $a5;
        $thisyear['a6'] = $a6;
        $thisyear['a7'] = $a7;
        $thisyear['a8'] = $a8;
        $thisyear['a9'] = $a9;
        $thisyear['a10'] = $a10;
        $thisyear['a11'] = $a11;
        $thisyear['a12'] = $a12;
        $thisyear['a13'] = $a13;
        $thisyear['a14'] = $a14;
        $thisyear['a15'] = $a15;
        $thisyear['a16'] = $a16;
        $thisyear['a17'] = $a17;
        $thisyear['ygcg'] = $ygcg;
        $thisyear['gwycgz'] = $gwycgz;
        $thisyear['gwycwh'] = $gwycwh;
        $thisyear['gwjdf'] = $gwjdf;
        $thisyear['sgtotal'] = $ygcg + $gwycgz + $gwycwh + $gwjdf;
        $thisyear['jgyxjf'] = $jgyxjf;
        $thisyear['project_count'] = $project_count;
        $thisyear['project_payment'] = $project_payment;


        $total = 0;
        $general_public_budget = 0;
        $government_budgets = 0;
        $institution_other_income = 0;
        $jbzc = 0;
        $xmzc = 0;
        $fiscal_appropriation = 0;
        $shbzjyzc = 0;
        $wsjkzc = 0;
        $jnhbzc = 0;
        $cxsqzc = 0;
        $nlszc = 0;
        $zrzyhyqxzc = 0;
        $zfbzzc = 0;
        $zhfzzc = 0;
        $a1 = 0;
        $a2 = 0;
        $a3 = 0;
        $a4 = 0;
        $a5 = 0;
        $a6 = 0;
        $a7 = 0;
        $a8 = 0;
        $a9 = 0;
        $a10 = 0;
        $a11 = 0;
        $a12 = 0;
        $a13 = 0;
        $a14 = 0;
        $a17 = 0;
        $ygcg = 0;
        $gwycgz = 0;
        $gwycwh = 0;
        $gwjdf = 0;
        $jgyxjf = 0;

        foreach ($lastyears as $item) {
            // if ($item['type'] == 1) continue;
            $total += $item['total'];
            $general_public_budget += $item['general_public_budget'];
            $government_budgets += $item['government_budgets'];
            $institution_other_income += $item['institution_other_income'];
            $fiscal_appropriation += $item['fiscal_appropriation'];
            $jbzc += $item['project_category_name'] == "基本支出" ? $item['total'] : 0;
            $xmzc += $item['project_category_name'] == "项目支出" ? $item['total'] : 0;
            $shbzjyzc += $item['functional_category_name_class'] == "社会保障和就业支出" ? $item['total'] : 0;
            $wsjkzc += $item['functional_category_name_class'] == "卫生健康支出" ? $item['total'] : 0;
            $jnhbzc += $item['functional_category_name_class'] == "节能环保支出" ? $item['total'] : 0;
            $cxsqzc += $item['functional_category_name_class'] == "城乡社区支出" ? $item['total'] : 0;
            $nlszc += $item['functional_category_name_class'] == "农林水支出" ? $item['total'] : 0;
            $zrzyhyqxzc += $item['functional_category_name_class'] == "自然资源海洋气象等支出" ? $item['total'] : 0;
            $zfbzzc += $item['functional_category_name_class'] == "住房保障支出" ? $item['total'] : 0;
            $zhfzzc += $item['functional_category_name_class'] == "灾害防治及应急管理支出" ? $item['total'] : 0;
            if ($item['functional_category_code_class'] == 208 && $item['functional_category_code_fund'] == 5) {
                if ($item['functional_category_code_item'] == 1) {
                    $a1 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 5) {
                    $a2 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 210 && $item['functional_category_code_fund'] == 11) {
                if ($item['functional_category_code_item'] == 1) {
                    $a3 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 3) {
                    $a4 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 211 && $item['functional_category_code_fund'] == 4) {
                if ($item['functional_category_code_item'] == 1) {
                    $a5 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 211 && $item['functional_category_code_fund'] == 6) {
                if ($item['functional_category_code_item'] == 99) {
                    $a6 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 213 && $item['functional_category_code_fund'] == 2) {
                if ($item['functional_category_code_item'] == 5) {
                    $a7 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 99) {
                    $a8 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 220 && $item['functional_category_code_fund'] == 1) {
                if ($item['functional_category_code_item'] == 1) {
                    $a9 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 6) {
                    $a10 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 50) {
                    $a11 += $item['general_public_budget'];
                }
                if ($item['functional_category_code_item'] == 99) {
                    $a12 += $item['general_public_budget'];
                }
            }
            if ($item['functional_category_code_class'] == 221 && $item['functional_category_code_fund'] == 2) {
                if ($item['functional_category_code_item'] == 1) {
                    $a13 += $item['general_public_budget'];
                }
            }

            if ($item['functional_category_code_class'] == 224 && $item['functional_category_code_fund'] == 1) {
                if ($item['functional_category_code_item'] == 4) {
                    $a14 += $item['general_public_budget'];
                }
            }
            if ($item['economics_category_code'] == "30212") {
                $ygcg += $item['general_public_budget'];
            }
            if (in_array($item['economics_category_code'], [30913, 31013])) {
                $gwycgz += $item['general_public_budget'];
            }
            if ($item['economics_category_code'] == "30231") {
                $gwycwh += $item['general_public_budget'];
            }
            if ($item['economics_category_code'] == "30217") {
                $gwjdf += $item['general_public_budget'];
            }
            if ($item['functional_category_code_class'] == 212 && $item['functional_category_code_fund'] == 8) {
                if ($item['functional_category_code_item'] == 16) {
                    $a17 += $item['general_public_budget'];
                }
            }
            if ($item['economics_category_code_class'] == "302") {
                $jgyxjf += $item['fiscal_appropriation'];
            }
        }
        $lastyear['total'] = $total;
        $lastyear['general_public_budget'] = $general_public_budget;
        $lastyear['government_budgets'] = $government_budgets;
        $lastyear['institution_other_income'] = $institution_other_income;
        $lastyear['fiscal_appropriation'] = $fiscal_appropriation;
        $lastyear['jbzc'] = $jbzc;
        $lastyear['xmzc'] = $xmzc;
        $lastyear['shbzjyzc'] = $shbzjyzc;
        $lastyear['wsjkzc'] = $wsjkzc;
        $lastyear['jnhbzc'] = $jnhbzc;
        $lastyear['cxsqzc'] = $cxsqzc;
        $lastyear['nlszc'] = $nlszc;
        $lastyear['zrzyhyqxzc'] = $zrzyhyqxzc;
        $lastyear['zfbzzc'] = $zfbzzc;
        $lastyear['zhfzzc'] = $zhfzzc;
        $lastyear['a1'] = $a1;
        $lastyear['a2'] = $a2;
        $lastyear['a3'] = $a3;
        $lastyear['a4'] = $a4;
        $lastyear['a5'] = $a5;
        $lastyear['a6'] = $a6;
        $lastyear['a7'] = $a7;
        $lastyear['a8'] = $a8;
        $lastyear['a9'] = $a9;
        $lastyear['a10'] = $a10;
        $lastyear['a11'] = $a11;
        $lastyear['a12'] = $a12;
        $lastyear['a13'] = $a13;
        $lastyear['a14'] = $a14;
        $lastyear['a17'] = $a17;
        $lastyear['ygcg'] = $ygcg;
        $lastyear['gwycgz'] = $gwycgz;
        $lastyear['gwycwh'] = $gwycwh;
        $lastyear['gwjdf'] = $gwjdf;
        $lastyear['sgtotal'] = $ygcg + $gwycgz + $gwycwh + $gwjdf;
        $lastyear['jgyxjf'] = $jgyxjf;

        // 标题二
        $calc['v1'] = $thisyear['total'] == 0 ? 0 : ($thisyear['general_public_budget'] / $thisyear['total']) * 100;
        // var_dump($calc['v1'],$thisyear['general_public_budget'] / $thisyear['total']);exit;
        $calc['v2'] = $thisyear['general_public_budget'] - $lastyear['general_public_budget'];
        $calc['v3'] = $thisyear['general_public_budget'] == 0 ? 0 : 100 * (($thisyear['general_public_budget'] - $lastyear['general_public_budget']) / $thisyear['general_public_budget']);

        $calc['v4'] = $thisyear['total'] == 0 ? 0 : ($thisyear['government_budgets'] / $thisyear['total']) * 100;
        $calc['v5'] = $thisyear['government_budgets'] - $lastyear['government_budgets'];
        $calc['v6'] = $thisyear['government_budgets'] == 0 ? 0 : 100 * (($thisyear['government_budgets'] - $lastyear['government_budgets']) / $thisyear['government_budgets']);

        $calc['v7'] = $thisyear['total'] == 0 ? 0 : ($thisyear['institution_other_income'] / $thisyear['total']) * 100;
        $calc['v8'] = $thisyear['institution_other_income'] - $lastyear['institution_other_income'];
        $calc['v9'] = $thisyear['institution_other_income'] == 0 ? 0 : 100 * (($thisyear['institution_other_income'] - $lastyear['institution_other_income']) / $thisyear['institution_other_income']);

        // 标题三
        $calc['v10'] = $thisyear['total'] == 0 ? 0 : ($thisyear['jbzc'] / $thisyear['total']) * 100;
        $calc['v11'] = $thisyear['jbzc'] - $lastyear['jbzc'];
        $calc['v12'] = $thisyear['jbzc'] == 0 ? 0 : 100 * (($thisyear['jbzc'] - $lastyear['jbzc']) / $thisyear['jbzc']);

        $calc['v13'] = $thisyear['total'] == 0 ? 0 : ($thisyear['xmzc'] / $thisyear['total']) * 100;
        $calc['v14'] = $thisyear['xmzc'] - $lastyear['xmzc'];
        $calc['v15'] = $thisyear['xmzc'] == 0 ? 0 : 100 * (($thisyear['xmzc'] - $lastyear['xmzc']) / $thisyear['xmzc']);

        // 标题四

        // 标题五
        // $calc['v15'] = $thisyear['jbzc'] - $lastyear['jbzc'];
        $calc['v16'] = $thisyear['jbzc'] == 0 ? 0 : 100 * (($thisyear['jbzc'] - $lastyear['jbzc']) / $thisyear['jbzc']);
        $calc['v17'] = $thisyear['xmzc'] - $lastyear['xmzc'];
        $calc['v18'] = $thisyear['xmzc'] == 0 ? 0 : 100 * (($thisyear['xmzc'] - $lastyear['xmzc']) / $thisyear['xmzc']);

        $calc['v19'] = $thisyear['total'] == 0 ? 0 : ($thisyear['shbzjyzc'] / $thisyear['total']) * 100;
        $calc['v20'] = $thisyear['total'] == 0 ? 0 : ($thisyear['wsjkzc'] / $thisyear['total']) * 100;
        $calc['v21'] = $thisyear['total'] == 0 ? 0 : ($thisyear['jnhbzc'] / $thisyear['total']) * 100;
        $calc['v22'] = $thisyear['total'] == 0 ? 0 : ($thisyear['nlszc'] / $thisyear['total']) * 100;
        $calc['v23'] = $thisyear['total'] == 0 ? 0 : ($thisyear['zrzyhyqxzc'] / $thisyear['total']) * 100;
        $calc['v24'] = $thisyear['total'] == 0 ? 0 : ($thisyear['zfbzzc'] / $thisyear['total']) * 100;
        $calc['v25'] = $thisyear['total'] == 0 ? 0 : ($thisyear['zhfzzc'] / $thisyear['total']) * 100;

        $calc['v26'] = $thisyear['a1'] - $lastyear['a1'];
        $calc['v27'] = $thisyear['a1'] == 0 ? 0 : (($thisyear['a1'] - $lastyear['a1']) / $thisyear['a1']) * 100;

        $calc['v28'] = $thisyear['a2'] - $lastyear['a2'];
        $calc['v29'] = $thisyear['a2'] == 0 ? 0 : (($thisyear['a2'] - $lastyear['a2']) / $thisyear['a2']) * 100;

        $calc['v30'] = $thisyear['a3'] - $lastyear['a3'];
        $calc['v31'] = $thisyear['a3'] == 0 ? 0 : (($thisyear['a3'] - $lastyear['a3']) / $thisyear['a3']) * 100;

        $calc['v32'] = $thisyear['a4'] - $lastyear['a4'];
        $calc['v33'] = $thisyear['a4'] == 0 ? 0 : (($thisyear['a4'] - $lastyear['a4']) / $thisyear['a4']) * 100;

        $calc['v34'] = $thisyear['a5'] - $lastyear['a5'];
        $calc['v35'] = $thisyear['a5'] == 0 ? 0 : (($thisyear['a5'] - $lastyear['a5']) / $thisyear['a5']) * 100;

        $calc['v36'] = $thisyear['a6'] - $lastyear['a6'];
        $calc['v37'] = $thisyear['a6'] == 0 ? 0 : (($thisyear['a6'] - $lastyear['a6']) / $thisyear['a6']) * 100;

        $calc['v38'] = $thisyear['a7'] - $lastyear['a7'];
        $calc['v39'] = $thisyear['a7'] == 0 ? 0 : (($thisyear['a7'] - $lastyear['a7']) / $thisyear['a7']) * 100;

        $calc['v40'] = $thisyear['a8'] - $lastyear['a8'];
        $calc['v41'] = $thisyear['a8'] == 0 ? 0 : (($thisyear['a8'] - $lastyear['a8']) / $thisyear['a8']) * 100;

        $calc['v42'] = $thisyear['a9'] - $lastyear['a9'];
        $calc['v43'] = $thisyear['a9'] == 0 ? 0 : (($thisyear['a9'] - $lastyear['a9']) / $thisyear['a9']) * 100;

        $calc['v44'] = $thisyear['a10'] - $lastyear['a10'];
        $calc['v45'] = $thisyear['a10'] == 0 ? 0 : (($thisyear['a10'] - $lastyear['a10']) / $thisyear['a10']) * 100;

        $calc['v46'] = $thisyear['a11'] - $lastyear['a11'];
        $calc['v47'] = $thisyear['a11'] == 0 ? 0 : (($thisyear['a11'] - $lastyear['a11']) / $thisyear['a11']) * 100;

        $calc['v48'] = $thisyear['a12'] - $lastyear['a12'];
        $calc['v49'] = $thisyear['a12'] == 0 ? 0 : (($thisyear['a12'] - $lastyear['a12']) / $thisyear['a12']) * 100;

        $calc['v50'] = $thisyear['a13'] - $lastyear['a13'];
        $calc['v51'] = $thisyear['a13'] == 0 ? 0 : (($thisyear['a13'] - $lastyear['a13']) / $thisyear['a13']) * 100;

        $calc['v52'] = $thisyear['a14'] - $lastyear['a14'];
        $calc['v53'] = $thisyear['a14'] == 0 ? 0 : (($thisyear['a14'] - $lastyear['a14']) / $thisyear['a14']) * 100;

        $calc['v54'] = $thisyear['sgtotal'] - $lastyear['sgtotal'];
        $calc['v55'] = $thisyear['sgtotal'] == 0 ? 0 : (($thisyear['sgtotal'] - $lastyear['sgtotal']) / $thisyear['sgtotal']) * 100;

        $calc['v56'] = $thisyear['ygcg'] - $lastyear['ygcg'];
        $calc['v57'] = $thisyear['ygcg'] == 0 ? 0 : (($thisyear['ygcg'] - $lastyear['ygcg']) / $thisyear['ygcg']) * 100;

        $calc['v58'] = $thisyear['a17'] - $lastyear['a17'];
        $calc['v59'] = $thisyear['a17'] == 0 ? 0 : (($thisyear['a17'] - $lastyear['a17']) / $thisyear['a17']) * 100;

        $calc['v60'] = $thisyear['jgyxjf'] - $lastyear['jgyxjf'];
        $calc['v61'] = $thisyear['jgyxjf'] == 0 ? 0 : (($thisyear['jgyxjf'] - $lastyear['jgyxjf']) / $thisyear['jgyxjf']) * 100;

        $calc['v62'] = $thisyear['gwycgz'] == 0 ? 0 : (($thisyear['gwycgz'] - $lastyear['gwycgz']) / $thisyear['gwycgz']) * 100;
        $calc['v63'] = $thisyear['gwjdf'] == 0 ? 0 : (($thisyear['gwjdf'] - $lastyear['gwjdf']) / $thisyear['gwjdf']) * 100;
        $calc['v64'] = $thisyear['gwycwh'] == 0 ? 0 : (($thisyear['gwycwh'] - $lastyear['gwycwh']) / $thisyear['gwycwh']) * 100;
        foreach ($thisyear as $k => $v) {
            if ($k != "project_count") {
                $thisyear[$k] = $v / 10000;
            }
        }
        foreach ($lastyear as $k => $v) {
            $lastyear[$k] = $v / 10000;
        }

        foreach ($calc as $k => $v) {
            if (in_array($k, [
                'v2',
                'v5',
                'v8',
                'v11',
                'v14',
                'v17',
                'v26',
                'v28',
                'v30',
                'v32',
                'v34',
                'v36',
                'v38',
                'v40',
                'v42',
                'v44',
                'v46',
                'v48',
                'v50',
                'v52',
                'v54',
                'v56',
                'v58',
                'v60',
            ])) {
                $calc[$k] = $v / 10000;
            }
        }

        return [
            "thisyear" => $thisyear,
            "lastyear" => $lastyear,
            "calc" => $calc,
            "year" => date("Y")
        ];
    }

    public function judge($a)
    {
        if ($a >= 0) {
            return '增加';
        } else {
            return '减少';
        }
    }
    public function judge2($a)
    {
        if ($a >= 0) {
            return '增长';
        } else {
            return '下降';
        }
    }
    public function sabs($a)
    {
        if ($a < 0) {
            return -1 * $a;
        }
        return $a;
    }
}
